How to check which scene is loaded and write if code for it.
Hello, I wrote script for player movement. I use same script for different playable characters. Each different character is in different scenes. In player movement script I included double jump too, but I don't want to have first player double jump and I want to have other double jump. I want to write something like that, If(Scene2) {
}
So, how should I write if scene 2 is loaded statement? Sorry for my bad English, hope you understand what I meant.
Answer by Cepheid · Apr 19, 2016 at 12:35 PM
Hi there @Kobaxidze
To check what scene is currently open you can make use of the SceneManagement namespace provided by Unity. Using the SceneManager class you can retrieve the currently active scene in the game and store it into a temporary variable which you can then use to check either it's name or build index in a conditional. For example:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ExampleClass : MonoBehaviour
{
void Start ()
{
// Create a temporary reference to the current scene.
Scene currentScene = SceneManager.GetActiveScene ();
// Retrieve the name of this scene.
string sceneName = currentScene.name;
if (sceneName == "Example 1")
{
// Do something...
}
else if (sceneName == "Example 2")
{
// Do something...
}
// Retrieve the index of the scene in the project's build settings.
int buildIndex = currentScene.buildIndex;
// Check the scene name as a conditional.
switch (buildIndex)
{
case 0:
// Do something...
break;
case 1:
// Do something...
break;
}
}
}
Every time this object is started within a new scene it will grab the current scene and check it's name and build index which you can then use to perform conditional logic based upon the currently open scene. Below are some links to the Scripting API.
GetActiveScene() - Scripting API
I hope this helps! :)
void Update()
{
if (Scene$$anonymous$$anager.GetActiveScene () == Scene$$anonymous$$anager.GetSceneByName ("scene1"))
{
Scene$$anonymous$$anager.LoadScene ("scene2");
}
else if (Scene$$anonymous$$anager.GetActiveScene () == Scene$$anonymous$$anager.GetSceneByName ("scene2"))
{
Scene$$anonymous$$anager.LoadScene ("scene1");
}
}
well i figured this out.. might someone is interested for switching between scenes. i used it for my player. (ontrigger enter load the opposite scene and there on same trigger back to main scene)
Thank you for this. It helped me with the last part of my game.
Answer by JuanMaldonado · Aug 03, 2018 at 09:01 PM
SceneManager.GetSceneByName(string).isLoaded
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByName.html
Works with multiple scenes for the Editor and at runtime :)
Answer by IgorAherne · Oct 05, 2017 at 11:59 PM
I came to this thread in a search for an answer, but it didn't satisfy me - I need to check if the specific scene is currently opened in a hierarchy (there could be many scenes currently loaded).
So, I used one of two variants depending if I am in Editor or Runtime:
#if UNITY_EDITOR
bool ifScene_CurrentlyLoaded_inEditor(string sceneName_no_extention) {
for(int i=0; i< UnityEditor.SceneManagement.EditorSceneManager.sceneCount; ++i) {
var scene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneAt(i);
if(scene.name == sceneName_no_extention) {
return true;//the scene is already loaded
}
}
//scene not currently loaded in the hierarchy:
return false;
}
#endif
bool isScene_CurrentlyLoaded(string sceneName_no_extention) {
for(int i = 0; i<SceneManager.sceneCount; ++i) {
Scene scene = SceneManager.GetSceneAt(i);
if(scene.name == sceneName_no_extention) {
//the scene is already loaded
return true;
}
}
return false;//scene not currently loaded in the hierarchy
}
Answer by TheBosoYolo · Mar 15, 2021 at 05:29 PM
How @Cepheid said, you can do it allso by using
SceneManager.GetActiveScene().name
None of the other stuff seems to work, but here you are, being a hero, saving my day ;) thanks!
Answer by hihihihihihihihihi · Aug 16, 2019 at 12:51 PM
using UnityEngine;
using UnityEngine.SceneManagement;
public class Test : MonoBehaviour{
IEnumerator LoadSceneAdditive(int buildIndex)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(buildIndex, LoadSceneMode.Additive);
while (!operation.isDone)
{
yield return null;
}
//Do somethin
}
}