0

I am having some trouble while adding a load next level script to the play button. Play Script:

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class PlayScript : MonoBehaviour {

    void OnMouseDown()
    {
        SceneManager.LoadScene(1); //loads level 1

    }

}

I attached the above script to the Play button UI but when i click the button the level does not open! What's the error here? Confusing!

1 Answers1

1

The issue is that OnMouseDown() is only called for old style GUI (IGUI) elements and GameObjects with colliders attached. (see the docs)

What you need for this is to move your scene loading logic to another function, e.g. void LoadLevel(). Then you set up the UI button using the inspector to call that function.

1: Add a function to OnClick

Locate the Button component on your Play button object and click the plus at the bottom of the OnClick () area.

OnClick for a Button in the Unity inspector

2: Drag over the GameObject which has the script you'd like to run a function from.

In this case, I've just attached the script to the button itself. You should drop it on the left-hand box. Alternatively, you can click the circle to select the target GameObject from a list.

Assigning a target GameObject to the OnClick list

3: Use the dropdown to select the function

The dropdown will show you all scripts / components attached to the GameObject, then if you hover over one of those the available public functions on that script / component.

Picking the function to run from the dropdown

Joe
  • 398
  • 4
  • 15