cant access script in GameObject using Find
public int numPick;
public string name;
public float price;
public float day;
public string status;
void Start()
{
Button buttonCtrl = this.GetComponent<Button> ();
buttonCtrl.onClick.AddListener (() => myButtonDelegate ());
}
void myButtonDelegate()
{
GameObject.Find ("Canvas").GetComponent<MainField> ().newPage(2);
GameObject.Find ("addgoal").GetComponent<GoalEditor> ().findProvideInfo (name, price, day);
//AddPrefab.arrangeGoal (numPick);
}
this is instantiated button prefab. whenever i run through myButtonDelegate() function the second call "addgoal" give me error:Object reference not set to an instance of an object.
the difference from first and second call is that "canvas" is set from the beginning as setactive=true while "addgoal" is set to false but, when myButtonDelegate() is called the "canvas" is set to false through script and "addgoal" to true. Also, the "addgoal" gameobject is a child of a child of "canvas".
and I need to get into GoalEditor script
I also tried
void myButtonDelegate()
{
GameObject canvas = GameObject.Find ("Canvas");
canvas.GetComponent<MainField> ().newPage(2);
canvas.GetComponentInChildren<GoalEditor> ().findProvideInfo (name, price, day);
//AddPrefab.arrangeGoal (numPick);
}
but with same result, still cant access the GoalEditor scirpt.
Answer by senatin · Jan 17, 2017 at 08:13 PM
Oh After more search I finally found solution
void myButtonDelegate()
{
GameObject canvas = GameObject.Find ("Canvas");
canvas.GetComponent<MainField> ().newPage(2);
canvas.GetComponentInChildren<GoalEditor> (true).findProvideInfo (name, price, day);
//AddPrefab.arrangeGoal (numPick);
}
puting true will find also the inactive I leave this here someone might look for it.