- Home /
Question by
maxim6394 · Mar 14, 2015 at 08:11 AM ·
timingloadleveladditive
Problem with LoadLevelAdditive Timing
So I have a little problem which seems to me about the timing of LoadLevelAdditive. I'm instantiating a player object which in its start method instantiates another class that loads some UI from a seperate scene. in the same start method I try to excess that UI, but it is always null. when I output the contents of the UI scene it seems that it is only a valid object in the second update. how can I solve this?
Comment
Answer by LukaKotar · Mar 14, 2015 at 08:19 AM
You can make the Start()
function wait one frame before trying to access your variable using a co-routine:
IEnumerator Start(){ // Change the function type from 'void' to 'IEnumerator'
// Do something...
yield return null; //Wait one frame, then continue the function
// Access your variable
}
Update()
cannot be co-routine, but you can use simple boolean logic to skip one frame before trying to access it:
bool canAccess = false;
void Update(){
if(canAccess){
// Access your variable
}else{
// Set 'canAccess' to true. The above 'if' statement will run next Update
canAccess = true;
}
}