Question by
strikeric11 · Sep 07, 2016 at 12:46 PM ·
c#unity5
add variable to Instantiated objects then destroy it in another function
I want to add variable to instantiated prefabs and destroy it in another function calling that variable. Here's my code
void Start()
{
spawnmona();
}
public void spawnmona()
{
Vector3 animalpos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
carno = Random.Range(0, 4);
GameObject carr = Instantiate(car[carno], animalpos, transform.rotation);
}
Then I want to destroy that car by calling this function:
public void destroyclone()
{
Destroy(GameObject carr);
}
But this gives me a error, how can I do this correctly?
Comment
Best Answer
Answer by Sergio7888 · Sep 10, 2016 at 03:58 PM
public void destroyclone()
{
if(carr)//check if exists
{
Destroy(carr);//pass only the variable, not the type
}
}
Your answer
Follow this Question
Related Questions
Spawn added objects into a circle 1 Answer
Movement Unit (RTS) 0 Answers
Car from Unity's standard assets giving me of null reference errors 0 Answers
How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer
How do you move the camera with the player. Whats wrong with my code? 0 Answers