- Home /
Question by
pokemonsters · Dec 13, 2012 at 06:59 AM ·
variablechildrenconnectionparent-child
Declaring Variable in Child & Access as Parent
var x : int;
function Start() {
x = Random.Range(1, 100);
}
The above script is attached to a child game object. How do I access the same x in a script meant to be attached to the parent?
Comment
Answer by Tourist · Dec 13, 2012 at 10:15 AM
Make the variable x public.
In the parent script, get the child object
get the component script of your child object where x is declared (using GetComponent)
here you are
Answer by BiG · Dec 13, 2012 at 11:27 AM
If you have a unique child that exhibits that variable, you can use GetComponentInChildren.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple objects sharing a boolean variable in a script. 1 Answer
Simple enemy kill count 1 Answer
Unity3d Javascript Reference Script Variable 1 Answer
Activate children 2 Answers