- Home /
how to access scripts in children
so how to access scripts in children dos anyone know in a child there is a script called enemy and in enemy there is a var called health anythone know how to change that from the parent
gameobject-->child-->script called enemy-->var in enemy called health
i just want the health = 100
Answer by Trent · Feb 07, 2011 at 03:26 AM
There are a lot of ways to do this here are two:
Here is my preferred method (only if the script you are looking for occurs only once in the children)
enemy myscript = gameObject.GetComponentInChildren<enemy>(); enemy.health = 90;
This is a little bit more specific method (specifies the child gameobjects name)
enemy myscript = GameObject.Find("child").GetComponent<enemy>(); enemy.health = 90;
[Edited] to show symbols
Learn more here:
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Answer by tertle · Feb 07, 2011 at 03:16 AM
Accessing components - for scripts on the same object
Accessing game objects - for scripts on a different object
Or maybe slightly more relevant
Your answer
Follow this Question
Related Questions
access vector variable from script 1 Answer
Access on child's trigger in parent script 1 Answer
How to access child GameObjects (NOT Transforms)? 2 Answers
Update Parent/ Children From Script? 2 Answers
Script to add script to all children 1 Answer