- Home /
How do I delete a gamebject that is not the one my script is in?
I have a script that destroys the gameobject the script is in when a variable reaches 0. I really need to destroy the parent though.
So Object A is inside of Object B. The script is inside of Object A. I need to destroy Object B. Can anyone help me out?
Answer by KiraSensei · Aug 08, 2012 at 02:51 PM
Create a variable and give it to eat to your script !
In your script :
var myObjectToDelete:GameObject;
function Update()
{
Destroy(myObjectToDelete);
}
Don't forget to put your game object in the inspector of the script. This should work.
Very important : put the destroy line at the very end, if you do something after, there will be errors ...
Thank you! I kind of forgot about this but forgot the lines of code for it anyways. It's working great, much appreciated.
Actually, @$$anonymous$$iraSensei, Destroy is delayed at the end of the current update.
Yes, thanks for telling it :). Please validate the answer if it's ok @Doneyes !
At the left of answers, you have thumbs up, a number, thumbs down and a check icon, click on the check icon :)
Answer by screwworkn · Aug 08, 2012 at 02:57 PM
I haven't tested this code but I believe you should be able to do the following
GameObject ObjectB = ObjectA.transform.parent.gameObject;
Then you can destroy ObjectB
Your answer
Follow this Question
Related Questions
Getting all Object to go next scene. 1 Answer
Trying to Destroy Object when Wave starts 1 Answer
particle effect activation 1 Answer
Destroy object problem. 1 Answer
How to make an event happen after an object is collided with 2 Answers