Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by Owen-Rowley · Oct 19, 2017 at 10:40 PM · scripting problemscript.scene

Loading Next Scene

Hello I am having trouble to load next level/scene using a script that check if player had collided a cube. I have twenty different scene naming map1, map2, map3 and so on.

I can't find and write a script foe that, can anyone please help? thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by witylernn · Oct 20, 2017 at 02:49 AM

This is an okay option, just repetitive

     string currentScene;
     private void Update()
     {
         if (collidedWithCube)
         {
             currentScene = SceneManager.GetActiveScene().name;
 
             switch (currentScene)
             {
                 case "map1":
                     SceneManager.LoadScene("map2");
                     break;
 
                 case "map2":
                     SceneManager.LoadScene("map3");
                     break;
 
                 //Continue on like this for all of your levels.
             }
         }
     }

This is a better option of you stick to the map1, map2, map3 naming convention,

     string currentScene;
     string spliced;
 
     int levelNumber;
     private void Update()
     {
         if (collidedWithCube)
         {
             currentScene = SceneManager.GetActiveScene().name;
             spliced = currentScene.Substring(currentScene.Length - 1, 1); // this gets you the last character of the scene name i.e "1", "2", etc
             levelNumber = int.Parse(spliced) + 1;
 
             SceneManager.LoadScene("map" + levelNumber.ToString());
         }
     }


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 Hellium · Oct 20, 2017 at 08:08 AM 0
Share

And what happens if there is map12? ;)

avatar image witylernn Hellium · Oct 20, 2017 at 02:36 PM 0
Share

So do me a favor and write 5 lines of code that also grabs the second to last character in the string, check if its a number, if it is, then use it.

Thanks, buddy.

avatar image Hellium witylernn · Oct 20, 2017 at 03:40 PM 1
Share

How about 3 lines ?

         string sceneName = Scene$$anonymous$$anager.GetActiveScene().name ;
         int levelNumber = int.Parse(System.Text.RegularExpressions.Regex.$$anonymous$$atch(sceneName, @"map(\d+)" ).Groups[1].Value) + 1;
         Scene$$anonymous$$anager.LoadScene("map" + levelNumber.ToString());

Or even 1 :

         Scene$$anonymous$$anager.LoadScene("map" + (int.Parse(System.Text.RegularExpressions.Regex.$$anonymous$$atch(Scene$$anonymous$$anager.GetActiveScene().name, @"map(\d+)" ).Groups[1].Value) + 1).ToString());
Show more comments
avatar image
0

Answer by Owen-Rowley · Oct 22, 2017 at 10:20 PM

Okay, sorry guys; I've tried put that in cube but it doesn't work...

I guess something like that cube detect if player (with the tag "Player") had collided that cube itself, these script should work but didn't??

remember i am very novice so may i ask you to put whole script ready so i can copy and paste? I know i sound very lazy but for good reason tho,m i always screwed up if i tried it myself...

Anyway thanks!

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

133 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 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

Teleporting problems 1 Answer

How can I handle scripts correctly? What's a good way to apply scripts to GameObjects? 1 Answer

[Question] Unity C# Set a Player Class and refence issue 0 Answers

Reload Current Scene with Scene-manager 3 Answers

how can i save highest played level?,how can i save number of highest played level? 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