- Home /
 
Getting an error while trying to get scene name
So I want to check if the player is in a specific level in the game I have been creating so that certain scripts can execute. However, I can't quite seem to figure out how to get the scene's name as a string. So far, this is the best I have done:
 public string scene;
 public bool inHouse = false;
 
 void Start(){
         scene = Scene.name;
     }
 
 
 void Update () {
         {
             if (scene = "House") {
                 inHouse = true;
             }
 }
 
               With this set up, I have been getting two errors, which are as follows:
Assets/Scripts/PlayerActivate.cs(34,31): error CS0120: An object reference is required to access non-static member UnityEngine.SceneManagement.Scene.name' Assets/Scripts/PlayerActivate.cs(60,25): error CS0029: Cannot implicitly convert type string' to `bool'
So what am I doing wrong? If you find out what to do, please elaborate, as I am still a beginner trying to understand c# :P
Answer by TBruce · Apr 29, 2016 at 03:54 PM
You either need to add a uses clause like so
 using UnityEngine.SceneManagement;
 
               or call Scene.name like so
 scene = UnityEngine.SceneManagement.Scene.name;
 
               also
 if (scene = "House") {
     inHouse = true;
 }
 
               needs to be
 if (scene == "House") {
     inHouse = true;
 }
 
               But depending on your game you could do it more efficiently like this
 inHouse =  (scene == "House");
 
              I'm still getting this as an error for the following line:
 scene = Scene.name;
 
                  Assets/Scripts/PlayerActivate.cs(37,31): error CS0120: An object reference is required to access non-static member `UnityEngine.Scene$$anonymous$$anagement.Scene.name'
$$anonymous$$eep in $$anonymous$$d that I do indeed have the uses clause needed.
Actually you should be using Scene$$anonymous$$anager. For example
 using UnityEngine.Scene$$anonymous$$anagement;
 string scene;
 scene = Scene$$anonymous$$anager.GetActiveScene().name;
 
                   or
 string scene;
 scene = UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.GetActiveScene().name;
 
                   But, to return Scene.name you need to create an instance of Scene like so
 Scene currentScene = new Scene();
 scene = currentScene.name;
 
                   on testing this scene returns an empty string where Scene$$anonymous$$anager.GetActiveScene().name returns the name of the active scene or level. Personally I have never tried using an instance of scene and all documentation says that one should use Scene$$anonymous$$anager.
Sorry for the delay! I do want to say that you helped me in more ways than one with that previous reply of yours; you taught me how to deal with scenes as well as how namespaces work :P
Your answer
 
             Follow this Question
Related Questions
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
Universal Level Changer with SceneMnager 1 Answer
Help with script 1 Answer
Error In Lives While Respawning 1 Answer