- Home /
How to load Level using scene object??
I've stored scene objects like this UnityEngine.Object[] Scenes;
but Application.LoadLevel() take int or levelName ... So is there any other way to load level using scene objects??
What do you mean by scene object? Unity levels exist as .unity3d files. LoadLevel() can take only an int or levelName because you can only load levels that are included in your build (using build settings dialog)
Unity only knows the index (int) of these levels and their name (levelName) They do not exist as objects.
thanks guys .. i didnt knw that was unityfiles , so i thought maybe call them objects ...
Answer by meat5000 · Aug 15, 2014 at 01:52 PM
You can simply place a script on said object with your LoadLevel call and the activating logic.
Answer by michaelkielstra · Aug 15, 2014 at 02:47 PM
Application.loadLevel() uses Unity Scenes (.unity files). These are different from objects or gameObjects -- a Unity Scene contains multiple gameObjects. Therefore, you have two choices. First, if you don't absolutely have to use your UnityEngine.Object[] scenes array, you can just put each scene object in a different .unity file (Unity scene). The other option is to create prefabs from each of your objects. Then, write a script which instantiates these prefabs. Keep track of which prefab is instantiated and, when you instantiate a new one, destroy the old one. Here's some sample code (c#):
public GameObject[] scenes; //The different scenes. Load these as prefabs in the inspector.
private GameObject instantiated = null; //Keep track of the instantiated prefab.
void instantiate(int n){
if(instantiated != null) GameObject.Destroy(instantiated); //Destroy the old scene.
instantiated = GameObject.Instantiate(scenes[n]); //Instantiate the new scene.
}
Apologies if this is slightly inaccurate, but it gets the concept across.