Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
10
Question by Kobaxidze · Apr 19, 2016 at 11:41 AM · script.

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
27
Best Answer

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

Scene Class - Scripting API

I hope this helps! :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kobaxidze · Apr 19, 2016 at 03:30 PM 0
Share

Thank you so much!

avatar image dan_wipf · Sep 28, 2017 at 07:04 PM 1
Share
       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)

avatar image $$anonymous$$ · Nov 07, 2017 at 11:47 AM 0
Share

Thank you for this. It helped me with the last part of my game.

avatar image
21

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
10

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
     }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by TheBosoYolo · Mar 15, 2021 at 05:29 PM

How @Cepheid said, you can do it allso by using

SceneManager.GetActiveScene().name

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cosmology27 · Apr 29, 2021 at 01:08 AM 0
Share

None of the other stuff seems to work, but here you are, being a hero, saving my day ;) thanks!

avatar image
0

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
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use fill method on prefab object? 0 Answers

Rotation problems 0 Answers

[Absolute beginner] Play animation on trigger (trigger = several fallen objects) script 1 Answer

"Method must have a return type" 1 Answer

plz help me in Trivia Quiz Game Template setup 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges