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?

  • 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

If you have a unique child that exhibits that variable, you can use GetComponentInChildren.