Search Unity

How to load a scene with a button?

Discussion in 'Scripting' started by Hectorastacio1, Dec 8, 2017.

  1. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    This is my current code, how can I fix this? I'm even getting an error on the GetMouseButtonDown

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SceneLoader : MonoBehaviour
    5. {
    6.  
    7.     void Update()
    8.     {
    9.         if (Input.GetMouseButtonDown)
    10.         {
    11.             Application.LoadLevel("1");
    12.         }
    13.     }
    14. }
     
  2. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    it says that Application is obsolete and I couldn't find any other example online that didn't use Application
     
  3. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    You should use the SceneManager to load scenes: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html

    Example:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SceneLoader : MonoBehaviour
    4. {
    5.     void Update()
    6.     {
    7.         if (Input.GetMouseButtonDown)
    8.         {
    9.             UnityEngine.SceneManagement.SceneManager.LoadScene(1);
    10.         }
    11.     }
    12. }
    13.  
     
    Hectorastacio1 likes this.
  4. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    I tried adding this script to the button and there is no compiler errors yet i press the button and it doesn't change scenes
     
  5. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    Did you add the scenes in the Build Settings of your project? Go to File > Build Settings and add all the scenes you need.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    Note that LoadScene(1) requires the proper index. If 1 isn't the proper target, then you may not get the desired effect.

    Generally I prefer the string version

    LoadScene("someSceneName") where you use the scene name instead as it doesn't matter what order your scenes are in your build settings. This will also throw an error if your scene is missing from your build settings, which is nice if you forgot to add it.

    Granted, if your scene name is "1", then still use the string instead of the int. (but you should probably not name a scene "1".
     
  7. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    I did add them
     
  8. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    Can you explain in another way I didn't understand what you mean to say besides that I should change my scene name
     
  9. pk_Holzbaum

    pk_Holzbaum

    Joined:
    Jul 26, 2012
    Posts:
    84
    Input.GetMouseButtonDown is a method and should be called with parentheses.

    Code (CSharp):
    1. Input.GetMouseButtonDown(0)
    0 is the identifier for a left click.
     
  10. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    If I write it this way I get error
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    So, you basically have a few ways of loading a scene. The two mentioned here are by index or string.

    So in build settings you have several levels. They are in order and if you call LoadScene(0) it loads the first level in the list. If I call LoadScene("Level1") it will load whatever Level1 is, no matter what order it is on the list.

    @pk_Holzbaum also mentioned is correct also. See documentation here https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html as well on the GetMouseButtonDown.
     
  12. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    Is this better
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. public class SceneLoader : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         if (Input.GetMouseButtonDown(0))
    8.  
    9.         {
    10.          
    11.             SceneManager.LoadScene("Cups");
    12.         }
    13.     }
    14. }
     
  13. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    I added this script to the button as you can see in the picture, yet it doesn't seem to work
     

    Attached Files:

  14. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    The code should be correct, but you can't check for input in Start. Put it back in Update()
     
    Hectorastacio1 likes this.
  15. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    That worked, thank you so much!
     
  16. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    Ok, let me change my answer. If you are putting this on a button, then you need to put it in it's own method and remove the input.GetMouseButtonDown check as you don't need that. Then you need to connect the method to the buttons OnClick.

    If this is foreign for you, I suggest looking into some tutorials. Unity has one here for buttons alone https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button?playlist=17111 but there are others.
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,603
    Glad it's working for you, just note that the code you are using will trigger no matter where you click on the screen, so you need to look at my answer above about hooking it to the button.
     
  18. Hectorastacio1

    Hectorastacio1

    Joined:
    Sep 11, 2017
    Posts:
    60
    I see this thanks I will look at the tutorial and see if i can get it to work with the on click