Hi, I am creating a 2D game (with the 2D tempelate). I want to spawn an object and for which I am using the following script. The script is attached to the object from which I want it to be spawned. But, when I run the game 3-4 objects spawn instead of 1. Please help me out. ASAP Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerSpawn : MonoBehaviour { public Transform spawnPoint; public GameObject playerPrefab; // Update is called once per frame void Update () { if(Input.GetKey(KeyCode.A)) { Spawn(); } } void Spawn() { Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation); } }
You have this running in update and using GetKey that means it is going to spawn stuff really fast while you hold the button down. Use GetKeyDown or GetKeyUP not getkey.