Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Stealthygolem · Oct 09, 2014 at 12:10 PM · c#scenechange

How do I load a new scene based on current scene?

I want to use the same "DoorEnter.cs" script for every level in my game. Is there a way I can declare "if scene = scene0, LoadScene1();" ?

Here is my sample code. The commented out code is what I'm trying to figure out here:

     public void OnTriggerStay2D    (Collider2D other) {
         if(other.gameObject.name == "Player") {
             Debug.Log ("DoorEnter is triggered");
             if(Input.GetKey(KeyCode.F)) {
                 //if scene = scene 0, LoadScene1();
                 //if scene = scene 1. LoadScene2();
             }
         }
     }
 
     void LoadScene1 () {
         Application.LoadLevel ("scene1");
     }
 
     void LoadScene2 () {
         Application.LoadLevel ("scene2");
 }
Comment
Add comment · Show 3
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 Stealthygolem · Oct 09, 2014 at 12:16 PM 0
Share

I feel like a fool. I tried loadedLevelName earlier but just had a syntax error, appearentyl! Thank you for the comment and help, cheers!

avatar image Landern · Oct 09, 2014 at 12:27 PM 1
Share

@Harshad$$anonymous$$, Sorry man, i tried to move your comment to an answer, but i think you set it not to everyone and is now lost in UA ether, very sorry.

avatar image HarshadK · Oct 09, 2014 at 12:54 PM 2
Share

@moses No issues. It happens. In the end, you were trying to help me. And that's what matters, helping each other. :-)

2 Replies

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

Answer by Stealthygolem · Oct 09, 2014 at 12:37 PM

Can you answer your own question? Maybe people will find it useful in the future:

     public void OnTriggerStay2D    (Collider2D other) {
         if(other.gameObject.name == "Player") {
             Debug.Log ("DoorEnter is triggered");
             if(Input.GetKey(KeyCode.F)) {
                 if(Application.loadedLevelName == "scene0") {
                     LoadScene1 ();
                 }
                 if(Application.loadedLevelName == "scene1") {
                     LoadScene2 ();
                 }
             }
         }
     }
 
     void LoadScene1 () {
         Application.LoadLevel ("scene1");
     }
 
     void LoadScene2 () {
         Application.LoadLevel ("scene2");
     }
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 Bunny83 · Oct 09, 2014 at 12:42 PM 2
Share

Yes, you can answer your own question ;)

Just for the record, you can also load a level by the level index. So to simply load the next level in order, just call:

 if (Application.loadedLevel + 1 < Application.levelCount)
     Application.LoadLevel( Application.loadedLevel + 1 );

However in more complex games where you might want to go from level 3 to level 7 it's easier to use a public variable with a the level name / index the script should load. This variable can be set in the inspector.

avatar image Landern · Oct 09, 2014 at 12:47 PM 1
Share

With the caveat of no points for self service, but the honor of helping the community.

avatar image
0

Answer by Redeemer86 · Oct 09, 2014 at 12:37 PM

 If(Application.loadedLevelName== "levelxname")
      Application.LoadLevel("levelxname");
 
 // or
 
 If(Application.loadedLevel == 1)
      Application.LoadLevel(2);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Health script, save health in between scenes. PlayerPrefs. 2 Answers

Distribute terrain in zones 3 Answers

Calling an Audio Source on one game object from a script on another game object..? 1 Answer

I want to set play scene of network lobby manger through script 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