• 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
0
Question by d-ostapa · Jun 24, 2016 at 05:02 AM · c#scene-switchingscene-change

Changing a scene with a click of an object?

Hi I am trying to make it so when I click on a certain object in my current scene it would change to another scene. I am trying to use

using UnityEngine.SceneManagement; using System.Collections;

public class NextLevel {

 void OnMouseDown ()
 {
     SceneManager.LoadScene(Level2);
 }
 
 

}

Whenever I do that it says Level2 does not exist in the current context.. I don't know if i'm doing it wrong or what. I have a scene named Level2 in my scenes folder. Any help would be appreciated.

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 corn · Jun 24, 2016 at 08:59 AM

Your forgot the double quotes, so Level2 is interpreted as a variable, but there is no declared variable called Level2 in your script. Hence this variable does not exist and it cannot be called.

You need to either use "Level2" as a string or create a Level2 variable. You also need to make NextLevel inherit from MonoBehaviour, or you will not be able to use it in your scene.

 using UnityEngine;
 using UnityEngine.SceneManagement; 
 
 public class NextLevel : MonoBehaviour
 {
      void OnMouseDown ()
      {
          SceneManager.LoadScene("Level2");
      }
 }

But assigning a variable in the inspector would be better practice, as it will allow you to change the scene to be loaded without modifying the script.

 using UnityEngine;
 using UnityEngine.SceneManagement; 
 
 public class NextLevel : MonoBehaviour
 {
      [SerializeField]
      private string _level2 = "Level2";

      void OnMouseDown ()
      {
          SceneManager.LoadScene(_level2);
      }
 }
Comment
Add comment · Show 2 · 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 d-ostapa · Jun 24, 2016 at 02:30 PM 0
Share

It works, besides the fact that it says "The type or namespace name '$$anonymous$$onoBehavior' could not be found" I don't think that it is supposed to do that...

avatar image d-ostapa d-ostapa · Jun 24, 2016 at 02:45 PM 0
Share

Never$$anonymous$$d I found a fix. Thanks for the help!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

loading of scene with UI 0 Answers

Object cant be found in new scene 0 Answers

Changing player position when changing scene 1 Answer

Remember Toggle Button State When Scene Return 0 Answers

Script is detecting input with ZERO actions 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