- Home /
Question by
DeathbyDomination · Aug 20, 2017 at 12:44 PM ·
script.parentchild object
Add script to parent if childobject is destroyed
Is there a way to add a script to a parent object when the child object becomes destroyed ?
Comment
Best Answer
Answer by Destolos · Aug 20, 2017 at 03:01 PM
void OnDestroy()
{
transform.parent.gameobject.AddComponent<YourScript>();
}
This creates a new script on the parent gameobject. I think you have to copy the values of members manual, but perhaps there is another way.
Answer by hfiani · Aug 20, 2017 at 02:55 PM
you can try this:
void OnDestroy()
{
transform.parent.gameObject.AddComponent<SCRIPTNAME> ();
//if you need to change some public variable
transform.parent.gameObject.GetComponent<SCRIPTNAME> ().variable = value;
}
Your answer
Follow this Question
Related Questions
Set child position and move parents with 2 Answers
GetComponentInChildren with tag in two variable 3 Answers
Shields 'always' inactive. Player object doesn't recognise if child exists(Solved) 1 Answer
Change coordinates of children and grandchildren of gameobject in panel 1 Answer
How can I get Compound Colliders to work properly? 2 Answers