Hello
I instantiate a game object and get it’s component after doing so.
The problem is then, the reference to the variables that were setup in Awake are null, but they can’t be if Awake was called.
So my code looks like this:
private IEnumerator CreatePoints(List<Points> points){
GameObject go = Instantiate(PreFabDatabase.Get(PointType));
go.transform.position = transform.position + new Vector3(4f,0f,5f);
go.transform.SetParent(transform);
PointerHandler pointerHandler = go.GetComponent<PointerHandler>();
yield return new WaitForEndOfFrame(); //give instantiation chance to set data
Debug.Log(pointerHandler.enterPos); // 0f,0f,0f why?
Debug.Log(pointerHandler.enterPoint); // null why?
}
My pointer handler class has this (it is a component of the gameObject i instantiated):
public PathPoint enterPoint;
public Vector3 enterPos;
void Awake(){
foreach(Transform T in gameObject.transform){
if(T.name == "Floor"){
enterPoint = new PathPoint("Wait",T.position);
Debug.Log(enterPoint.position); // works fine
enterPos = new Vector3(5f,5f,5f);
Debug.Log(enterPos); // 5f,5f,5f
break;
}
}
}
So why is the data not set when i get the component? I added a wait for frame assuming i was displaying data before it had chance to set… but that didn’t seem to solve it either… what would cause this problem?