• 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.

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

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

Keeping variables between scenes with scriptable objects? 0 Answers

Question Regarding Don'tDestroyOnLoad Object 1 Answer

Connect multiple scenes to one main scene 1 Answer

,Enums not being passed between scripts properly 1 Answer

Help with error message "Overwriting the same path as another open scene is not allowed" 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges