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 /
avatar image
0
Question by shepsaus000 · Apr 29, 2016 at 03:47 PM · scripting problemerrorscenescripting beginnername

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

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

1 Reply

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

Answer by TBruce · Apr 29, 2016 at 03:54 PM

@shepsaus000

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");
Comment
Add comment · Show 4 · 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 gjf · Apr 29, 2016 at 04:08 PM 0
Share

@$$anonymous$$avina - nice edit ;)

avatar image shepsaus000 · Apr 29, 2016 at 05:15 PM 0
Share

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.

avatar image TBruce shepsaus000 · Apr 29, 2016 at 05:35 PM 0
Share

@shepsaus000

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.

avatar image shepsaus000 TBruce · May 02, 2016 at 03:07 PM 0
Share

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

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

56 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

Related Questions

Whats the best method to change a script in one scene, without changing the same script in a different scene. 1 Answer

(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


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