Spawning an object on collision

I’d like to spawn a single sprite on collision with a set position. Currently when I spawn the sprite (as a prefab), it creates multiples of itself each time I pass the spawn point. How can I set the specific location for the spawned sprite and only instantiate it one time?

public class CreateObject : MonoBehaviour
{
     public Transform Spawnpoint;
     public RigidBody Prefab;

     void OnTriggerEnter()
     {

     RigidBody RigidPrefab;
     RigidPrefab = Instantiate(Prefab, Spawnpoint.position, Quaterion.identity) as RigidBody;

     }
}

From what I am reading, it sounds like you can accomplish this with a bool.

public class CreateObject : MonoBehaviour
{
     public Transform Spawnpoint;
     public RigidBody Prefab;
     private bool hasAlreadyCollided = false;
     void OnTriggerEnter()
     {
          if (!hasAlreadyCollided)
          {
               hasAlreadyCollided = true;
               RigidBody RigidPrefab;
               RigidPrefab = Instantiate(Prefab, Spawnpoint.position, Quaterion.identity) as RigidBody;
          }
     }
}
2 Likes