why does my instantiate sometimes dupe my prefab?
So im in the middle of making my 2D infinite runner using C# script and my SPIKE that is to destroy and Respawn my Player but it will sometimes duplicate my character 2-3 times if i land on the spike on an angle (both character and spike have box colliders but the spikes collider can be altered) i should also note Player is a prefab
If anyone has anything that could help me i would appreciate im an amateur hoping to improve
here is what i have so far
public class SPIKE1 : MonoBehaviour {
public GameObject Player;
public Transform SpawnPoint;
private bool respawn = false;
void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.tag == "Player")
Destroy(coll.gameObject);
if (respawn)
return;
if (coll.gameObject.tag == "Player")
Instantiate(Player, SpawnPoint.position, SpawnPoint.rotation);
SendMessage("respawn");
}
Answer by RybiconZX · Apr 12, 2016 at 07:04 AM
Try changing OnCollisionStay2D to OnCollisionEnter2D which should only happen once, the stay function runs every frame/update. When you request that the player get destroyed maybe it's not actually happening on that frame. Also what is the boolean respawn doing? I don't see where it would ever be changed to true but maybe you have that coded somewhere else.
thanks it fixed it, also the bool isnt anything to worry about
Your answer
Follow this Question
Related Questions
Destroying game object 0 Answers
Spawn in a different and random Spawn point from a list 0 Answers
Shooting an instantiated missile the way a object is facing. 0 Answers
Firing to a mouse position 0 Answers
I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer