- Home /
What is the state of GameObjects just after they have been loaded through a Application.LoadLevelAdditive() call?
Hi,
there is something I misunderstand about Application.LoadLevelAdditive(): If I try to get GameObjects loaded just after a call to this method located in a Start callback, GameObject.Find() returns null for any object. So It seems that loaded GameObjects are only "avalaible" one frame later. That might mean they are activated with a 1 frame delay after they have been loaded.
Ok, then what confuses me is that the Start method of every scripts attached to these objects gets called, letting me think we are still in the first frame.
So GameObjects can not be retrieved through GameObject.Find(), but their scipts seem to be executed. What is their real state? Active? Not active? Almost active?... :-)
Any explanation welcome!
yoyoooooooo
Answer by YoYoooooooo · Dec 11, 2013 at 01:42 PM
Ok, thanks for your contributions. Mixing both your answers, here is my finding: 1) loaded game objects are accessible through GameObject.Find() from the frame just after the frame that called LoadLevelAdditive() 2) callbacks such as OnEnable and Start are called int that same subsequent frame
My mistake was to think that the Start() callback was only called in the first frame.
Answer by Statement · Dec 09, 2013 at 03:29 PM
I was under the impression that the objects are loaded in last during the same frame, so they all exist on your next frame. That said, this is more of a guess and showing how you can deduce what happens yourself.
On the object that loads your scene, do a Debug.Log.
Use Time.frameCount to figure out what the current frame is during that call.On an object that is loaded in your additive scene, on Awake and Start, do a Debug.Log.
Use Time.frameCount to figure out what the current frame is during those calls.
Answer by Guts · Dec 09, 2013 at 03:44 PM
This is definitely NOT a stupid question! I wanted to find out myself what was going on, so I made 2 scenes: Scene1 and Scene2.
Scene1 is empty except for the main camera, to which I attach this script:
using UnityEngine;
using System.Collections;
public class LevelLoader : MonoBehaviour
{
void Start ()
{
Application.LoadLevelAdditive("Scene2");
GameObject findMe = GameObject.Find("FindMe");
if (findMe == null)
{
Debug.Log("FindMe was null in the same frame as the LoadLevelAdditive call.");
}
else
{
Debug.Log("FindMe was NOT null in the same frame as the LoadLevelAdditive call.");
}
StartCoroutine(CheckNextFrame());
}
IEnumerator CheckNextFrame()
{
yield return new WaitForEndOfFrame();
GameObject findMe = GameObject.Find("FindMe");
if (findMe == null)
{
Debug.Log("FindMe was null in the next frame after the LoadLevelAdditive call.");
}
else
{
Debug.Log("FindMe was NOT null in the next frame after the LoadLevelAdditive call.");
}
}
}
Scene2 is empty except for a Cube which I've renamed "FindMe". I attach this script to the FindMe object:
using UnityEngine;
using System.Collections;
public class FindMe : MonoBehaviour
{
void Awake()
{
Debug.Log("FindMe Awake");
}
void Start()
{
Debug.Log("FindMe Start");
}
void OnEnable()
{
Debug.Log("FindMe OnEnable");
}
}
When I play Scene1 (after making sure that I've added Scene1 and Scene2 to the build settings), I get the following output.
FindMe was null in the same frame as the LoadLevelAdditive call.
FindMe Awake
FindMe OnEnable
FindMe Start
FindMe was NOT null in the next frame after the LoadLevelAdditive call.