Spawn an object on key press C# Unity

spawn_26pm_josiahmunsey_coding

Create a prefab named objectIWantToSpawn in your resources folder.
Place this script on a gameobject in your scene and press space bar to spawn a clone of that prefab.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawn_Object_Keypress_Simple : MonoBehaviour
{
    Object prefabToSpawn;

    void Start()
    {
        prefabToSpawn = Resources.Load("objectIWantToSpawn");
        
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {

            Object clone;

            clone = Instantiate(prefabToSpawn, transform.position, transform.rotation);
                    

        }
    }
    
}
// easy unity code by Josiah Munsey 26PM

Leave a comment