Hi guys ! I have created this script to instantiate two set of coins in alternation, but the coins are not destroyed … all the coins have the tag “Pick Up”, but the script does not work to destroy them, something wrong? The result is that the sets do not alternate and overlap continuously
public GameObject coin;
public GameObject coinB;
public Transform coinPosition;
public float spawnTime = 40f;
public float spawnTimeB = 20f;
public float destroyTime = 20f;
void Start ()
{
startTime = Time.time;
InvokeRepeating ("Spawn", spawnTime, spawnTime);
InvokeRepeating ("SpawnB", spawnTimeB, spawnTimeB);
InvokeRepeating ("destroyCoin", destroyTime, destroyTime);
}
void Spawn ()
{
Instantiate (coin, coinPosition.position, coinPosition.rotation);
}
void SpawnB ()
{
Instantiate (coinB, coinPosition.position, coinPosition.rotation);
}
void destroyCoin()
{
Destroy(GameObject.FindWithTag("Pick Up"));
}
First of all I think it would be good if you made the event public
public void ...
second, are you looking for all the coins to destroy or only for 1 specific one
if you are looking for all the coins you can jsut do this
Foreach (GameObject coin in GameObject.FindWithTag("Pick Up")
{
Destroy (coin)
}
else I would make sure that the coin you are looking for has a thing different from the others
Foreach (GameObject coin in GameObject.FindWithTag("Pick Up")
{
if ('certain statement')
{
Destroy (coin)
}
}
Or what I think is the best solution for you make the event more specific so that not only an event is triggered but that a variable is send together with it
void destroyCoin (GameObject coin)
{
Destroy (coin)
}
if you use this make sure that when the event is triggerd that there is something like this
if ('statement')
{
GameObject.Find("").Getcomponent<'script'>.destroycoin(gameobject)
}
any question about what standing here/ what does this functions do jsut ask