Waiting for start() of component to finish.

I have a simple piece of code:

_ui = Instantiate(userInterface, Vector3.zero, Quaternion.identity) as GameObject;
_ui.name = "UI";

_uiScript = _ui.GetComponent<UISetup>();

I also have a Debug.Log(“UI SETUP START”); at the beggining of Start() method in UISetup class. The thing is, what I can see in my console is:


And also, a lot of stuff like null reference errors etc, because UISetup.AssingPlayer() uses stuff, that is initialized in Start().
I thought it is safe to assume, that after calling Instantiate(), Start() method of instantiated object and its components will be finished. Is there any pretty way for dealing with such situations? Maybe I could force waiting for Start() to be finished somehow?

I have no idea where your simple piece of code is being called from but it obviously happens before your other log “UI SETUP START”.

Maybe you’re calling that in Awake or OnEnable which happens before Start?

Attach the debugger and place breakpoints at each log and you should be able to piece together how to code flows.

As for your concern that _uiScript.AssignPlayer(_pcScript); is unsafe because it hasn’t executed its Start method, consider moving the code in that other script to Awake instead, which is called when the object is instantiated.

And as @EliteMossy points out, you can also define the execution order if you want to. You can also learn about the execution order here.