The question is answered, right answer was accepted
How do I stop multiple spawns?
I'm spawning alien ships as a vehicle goes through a trigger zone, but it spawns 11 versions of it.
I narrowed it down to the colliders on the vehicle but getting rid of them isn't an option.
Any suggestions how could I fix this issue?
I'm trying to make a train spawn a drone as it goes through a trigger.
Here is the code:
public class Spawn_Drone : MonoBehaviour
{
public Transform Drone;
public Transform SpawnPoint;
public Transform TeleportEffect;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "DroneActivator")
{
Spawn();
Debug.Log("Active 1: Spawn Drone script");
}
}
void Spawn()
{
Instantiate(Drone, SpawnPoint.position, SpawnPoint.rotation);
Instantiate(TeleportEffect, SpawnPoint.position, SpawnPoint.rotation); //addition
Debug.Log("Drone Active: Spawn Drone script");
}
}
it works when its an asset with 1 collider but when I add another collider to it that's when it makes multiples
There's the solution with lists that comes to $$anonymous$$d and it should work as long as the colliders are on the same game object (if not, you can tell me later and with a slight modification it can work with that too)
public class Spawn_Drone : MonoBehaviour
{
public Transform Drone;
public Transform SpawnPoint;
public Transform TeleportEffect;
public List<GameObject> activators = new List<GameObject>();
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "DroneActivator")
{
if(!activators.Contains(col.gameObject))
{
activators.Add(col.gameObject);
Spawn();
Debug.Log("Active 1: Spawn Drone script");
}
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "DroneActivator")
{
if(activators.Contains(col.gameObject))
{
activators.Remove(col.gameObject);
Debug.Log("collision ended, object removed from list");
}
else
{
Debug.Log("collision ended, but object was not in list");
}
}
}
void Spawn()
{
Instantiate(Drone, SpawnPoint.position, SpawnPoint.rotation);
Instantiate(TeleportEffect, SpawnPoint.position, SpawnPoint.rotation); //addition
Debug.Log("Drone Active: Spawn Drone script");
}
}
Even a bool could have worked here but i made it with a list so that you can even have multiple activator objects in case you ever need them....hope this helps!