How to load Level in Unity3d 4.6 above using Gui button

First create and GUI button. 

 

NewImage

 

1. Create an empty gamesObject

NewImage

 

2. After creating empy gameObject, click Add component and click new Script. Give the new script as LoadNewLevel and the language is set to csharp.

NewImage

 

3. Next edit the new script.

 

NewImage

 

4. Add the following code

 

    public void NextLevelLoad(int index)
    {
        Application.LoadLevel(index);
    }

    public void NextLevelLoad(string levelName)
    {
        Application.LoadLevel(levelName);
    }


NewImage

 

5.Click the button GUI that you have created and click + button on Click() section.

 

NewImage

 

6. Drag the empty gameObject that you created earlier nto the object field. Click the menu and select the LoadNewLevel -> NextLevelLoad (string) and enter the scene name in string that you want to load.

 

NewImage

 

Note 1: If you are using Unity 4.5 and below. Use the following code.

public void OnGUI()

 {

     if (GUI.Button(new Rect(10, 70, 50, 30), "Click"))

         Application.LoadLevel("level2");

 }



Note 2:



UnityEngine.Application.LoadLevel is obsolete and you should use SceneManager.LoadScene:

 

 using UnityEngine.SceneManagement;

 ...

 SceneManager.LoadScene("Scene_Name");


How to load Level in Unity3d 4.6 above using Gui button How to load Level in Unity3d 4.6 above using Gui button Reviewed by Admin on 3:02 PM Rating: 5

No comments:

Powered by Blogger.