Hi all, been struggling with this problem for a while now, probably missing something basic.
I’m instantiating a canvas that has some child objects attached to it and I want to add a script to one of the children:
void Start ()
{
//Spawn Stuff
SpawnPlayer();
eventSystem = Instantiate(eventSystemPrefab);
eventSystem.name = "EventSystem";
ui = Instantiate(uiPrefab);
ui.name = "UI";
SpawnUiContents();
}
Which leads to
public void SpawnUiContents()
{
JoystickManager JSMan = GameObject.Find("Handle").AddComponent<JoystickManager>();
//other code
}
The problem lies with the Start function of the code I’m adding which is simply:
void Start () {
startPos = transform.position;
}
But when I run it, startPos gets set to 0,0,0 because there is no transform. I assumed it hadn’t been created just yet but I read it should be.
It also seems to apply to other parts of the code (I later try and get the RectTransform of the canvas which also returns 0). Although I’ve called an instantiate it doesn’t seem to actually happen until later.
First time asking a question so apologies if I messed anything up and thanks a lot for any help.