- Home /
Setting variable as type "Scene"
Hey guys,
I want to make the type of my variable a scene, I have tried the following:
var sceneToLoad : Scene;
and
var SceneToLoad : scene;
but neither seems to work, can someone tell me what I have to type in for the variable type to make it so that you can enter a scene into the variable????
Thanks
-Grady
Answer by Mattivc · Jul 05, 2011 at 08:10 AM
There is no "Scene" object within Unity. The only way to work with scenes is to use: Application.loadedLevel and Application.loadedLevelName together with Application.LoadLevel.
For example
var sceneName : string = "MainMenu";
var sceneIndex : int = 0;
Application.LoadLevel(sceneName); // Load level by name
Application.LoadLevel(sceneIndex); // Load level by index number
i thought that might be how i would have to do it....
thanks anyway!!!!
thanks $$anonymous$$attivc , i had to make that clear in my $$anonymous$$d :)
Answer by Paul-Sinnett · Jul 30, 2014 at 08:42 AM
I wanted to do this because then doesn't matter if you decide to change the scene names in Unity. It turns out you can do it by using the base Object type:
public Object nextScene;
and then use the "name" member variable:
Application.LoadLevel(nextScene.name);
having said that, the scene objects don't seem to get packaged up with the rest of the data so I don't think I can use it
Be warned, storing an Object
with a scene will cause it to be stripped during your build creation. Seems Unity manually strips these references and replaces them with null.
Answer by Syndog · Jul 18, 2016 at 12:30 PM
This is a fairly dated question, so this may not have been true back when it was asked. But, for what it's worth, there is a Scene type located in UnityEngine.SceneManagement. The SceneManager class appears to be able to do what you're looking for...
using UnityEngine.SceneManagement;
.
.
.
Scene scene = SceneManager.GetSceneByName("Scene Name");
Hope this helps.
Answer by delstrega · Jul 03, 2012 at 02:19 PM
Actually there is a "scene type": EditorBuildSettingsScene. But its only available to editor scripts and only helps so much. But you can use this to add scenes to your build from script like shown here for example: http://answers.unity3d.com/questions/205742/adding-scene-to-build.html
But EditorBuildSettingsScene actually just string with an additional boolean to specify if it's included or not. It's an internal helper class for the editor. It's also in the UnityEditor namespace, so it doesn't exist at runtime.
Your answer
Follow this Question
Related Questions
Load 3d models with code, possible? 2 Answers
how to get other script variable from other scene? 3 Answers
Load a scene/level when enemy is close... 1 Answer
How to load a scene on collision 1 Answer
Load Scene When... 1 Answer