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
0
Question by Symbie · Jun 19, 2020 at 12:49 AM · buttonsscene-switchingscene change

Changing Scenes Efficiently

I am making a game, and it has around 32 levels. i have a scene which has a button, with the number level, eg a button with 1, and so on. at the moment i have a Script which loads the scene if the button is pressed, but having to write out 32 different voids going between each scene is a real hassle, is there a way i could do this better? This is the script that is for the buttons: using UnityEngine; using UnityEngine.SceneManagement;

 public class GameMaster : MonoBehaviour
 {
     public void GoToGameScene()
     {
         SceneManager.LoadScene("Level01");
     }
 
     public void Restart()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
     
     public void GoToMainMenu()
     {
         SceneManager.LoadScene("MainMenu");
     }
 
     public void LevelSelect()
     {
         SceneManager.LoadScene("LevelSelect");
     }
 }

the level select void is the scene with all the buttons. this is what it looks like alt text

i know it doesnt look the best, but its for testing purposes

asdf.png (42.5 kB)
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
Best Answer

Answer by haizathaneefa · Jun 19, 2020 at 02:57 AM

I am not sure if this is the best but create a function that gets the number from each button, something like:

 string number;
 
 public void getNumber()
 {
      number = gameObject.GetComponent<Text>().text;
  }


and then make a function to load the level while referencing your number level

    public void ChangeLevel()
     {
         SceneManager.LoadScene(number);
     }
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 SiniKettu_ · Jun 19, 2020 at 09:47 AM 0
Share

you need to change a bit the string, right ? Like

 string number;
  
  public void getNumber()
  {
       number = "Level"+(gameObject.GetComponent<Text>().text);
   }

Because @Symbie seems to use a nomenclature like "level" + level's number

In any case, "number" must be equals to targeted scene's name

avatar image Symbie SiniKettu_ · Jun 22, 2020 at 02:37 AM 0
Share

Thank you so much @haizathaneefa and @SiniKettu_ using both of your answers got me exactly what i wanted!

avatar image
0

Answer by Saiansh2525 · Jun 19, 2020 at 03:50 AM

I suggest that you use build index. Press build settings and add all your scenes in order and they will have a build index. Never use strings in scene management. Here is a link to that: https://docs.unity3d.com/ScriptReference/SceneManagement.Scene-buildIndex.html

Next, you should assign the function to each and every button with a Button parameter and just take the button's text convert it to a number and load the level with build indexes accordingly.

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

132 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

Related Questions

Switching between several levels 1 Answer

What is happening to scene manager.loadscene??? 2 Answers

Upgraded to 2017.1.0f3 and Now Scenes Are Not Loading Properly? 1 Answer

will switching scenes overwrite data? 2 Answers

Problem in loading death menu scene 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