- Home /
Destroy GameObjects with Tag ( Not found)
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"));
}
GameObject.FindWithTag returns the first gameobject with that tag it can find. There's no deter$$anonymous$$istic order in that. If you want to destroy specific coins in order, you can use a Queue, which removes the first element when dequeue is called on it.
no, i want destroy ALL game objects at the same time, then i spawn another set of coins, there is a way for this ?
then jsut do this Foreach (GameObject coin in GameObject.FindWithTag("Pick Up") { Destroy (coin) } what this does is It find all gameobjects with the tag and runs this piece of code for all of them
Answer by salamander555 · Apr 02, 2017 at 01:59 PM
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
i try this but not work :(, i want destroy all game object with tag "Pick Up" at the same time :)
public void destroyCoin()
{
foreach (GameObject coin in GameObject.FindWithTag("Pick Up")
{
Destroy(coin);
}
}
yeah my bad its not "FindwithTag" it search for something like "FindallobjectsWithtag"
YEA IT WOR$$anonymous$$ THAN$$anonymous$$ YOU !
Your answer
Follow this Question
Related Questions
Stop Coroutine itself? 2 Answers
C# library to detect the position of physical objects on the screen 1 Answer
Field Initializer Error 1 Answer
Scripts don't execute at all. 0 Answers
A question about references in C# 2 Answers