- Home /
Calling parent variables from child and visa versa
Hi i would like to know, how does a child call functions or variables from his/her parent? //currently my parent is a Sphere with a green light, and the child is a Sphere with a blue light (both is prefabs). And how does a parent call functions and variables from there children. (i want to do this later)
Ok this is what i have so far. [parent script: SpawnObjectLerp]
//global vars
public GameObject[] SpawnedControls;
public GameObject backPrefab;
public bool moveBack=false;
public int deviceCounter = 0;
private void Ams() {
SpawnedControls[deviceCounter] = (GameObject)Instantiate(backPrefab, transform.position, Quaternion.identity);
deviceCounter++;
for (int i=0;i<deviceCounter;i++)
{
Debug.Log("Adding gameobject to Parent");
SpawnedControls[i].transform.parent = transform.parent;
}
}
[The child prefab has a script attached to it with]
//globals
public GameObject pA;
public void OnMouseDown()
{
//string parentname = transform.parent.gameObject.name;
//bool test;
//pA = GameObject.Find(parentname);
//pA = GameObject.Find(transform.parent.gameObject);
//test= transform.parent.GetComponent<SpawnObjectLerp> ().moveBackFlag;
//pA.GetComponent<SpawnObjectLerp> ().moveBackFlag = true;
//pA.moveBackFlag = true;
//Debug.Log ("die kind" + parentName);
}
I mostly get the error message "Object reference not set to an instance of an object" I tried allot of different ways but I cant get past this part. All i want is access to the parent class script, to reach the moveBackFlag, and edit it. What i can do is create a empty game object and attached a moveVarsAround sqript to it. Then use this script to move var's back and forward. But i cant imagine this is the way to do it.
Hope someone can show me the right way. Thank you very much in advance.
Answer by MousePods · Mar 18, 2014 at 04:40 PM
First, the variable/method must be public
To get the a variable or call a method from the parent:
Script parentScript = this.transform.parent.GetComponent<Script>();
script.parentMethod();
or script.parentVariable
There are multiple ways to get the children's scripts. It depends on how many children you have.
// Get the first child in the children array
// use this if you only have one child as you won't
// know the order of the children's array)
this.transform.GetChild(0).GetComponent<Script>();
OR
// If you have multiple children
this.transform.FindChild("NameOfChild").GetComponent<Script>();
Hope this helps!
Thank you very much, it worked!!! And now i understand it a bit better :)
I also fixed the line where i assign objects to there parent to:
SpawnedControls[i].transform.parent=transform;
And my on$$anonymous$$ouseDown now looks like this.
public void On$$anonymous$$ouseDown()
{
Debug.Log ("$$anonymous$$y parent is:" + transform.parent.name);
SpawnObjectLerp parentScript = this.transform.parent.GetComponent<SpawnObjectLerp>();
parentScript.moveBackFlag = true;
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Access a child from the parent or other gameObject. 2 Answers
Accessing a variable from a child object and pass it to the parent 2 Answers
Access parents other child from other other child 1 Answer
Trouble accessing child 3 Answers