1
\$\begingroup\$

I've tried to attach this function to the play button.

public void Play () {
    Application.LoadLevel("Scene1");
}

I've created an empty Game Object and attached my script to it. But when I go to the OnClick() method in the Play button and try to select my Play() method can't find it, there is only a bunch of useless functions that do nothing to help me load the level.

And apparently most tutorials and forum posts suggest doing it this way, but it doesn't work for me.

Other posts say that you can assign the OnClick()-method in your script and show how to assign the function, but no one remembered to mention how to get the button object to which I could assign the function.

I'm new to unity and this problem is getting very frustrating. I'm using the free version of Unity for now.

\$\endgroup\$
1
  • \$\begingroup\$ Are you using the new Unity UI system? Because the steps you describe sound like the new UI, but you didn't actually specify that in your question. \$\endgroup\$
    – jhocking
    Jan 5, 2015 at 13:07

2 Answers 2

2
\$\begingroup\$

I am assumming you are trying to make a GameObject that, when clicked, will load another level. If so, continue on with this answer. If not, explain what you want in the comments below or in your question.

Note: In this case, I am going to use the OnMouseButton() method, since it would suit your needs perfectly


Steps

Step One: Preparation

I would make a Game Object that would act as the button
This includes:

  • A 3D Model with a Collider (Cube)
  • A 2D Sprite with a 2D Collider (Sprite)
  • An OnGUI Element: Note, OnGUI is redundant and most would recomend the next option
  • Unity 4.6 GUI Object

In this case, I will make an empty cube
enter image description here

Step Two: Coding

Now, create a new script or use an existing script for controlling the play button. In this case, I will make a script called PlayButton, but the following code will work on an existing script

using UnityEngine;
using System.Collections;

public class PlayButton : MonoBehaviour {
    void Start() {

    }

    void Update() {

    }
}

Now, what we are going to use is the method OnMouseDown(), which will run when the cursor clicks the game object. Also, we do not need the Start() and Update() methods, so we will remove them from the script.

using UnityEngine;
using System.Collections;

public class PlayButton : MonoBehaviour {
    void OnMouseDown() {
    }
}

From here we can do one of two things. We can directly load the next level inside this block of code, or we could call another block of code that would load the next level. I will do both for example sake. In the first example, we will add the code Application.LoadLevel("Scene1"), which will simply load the level Scene1

using UnityEngine;
using System.Collections;

public class PlayButton : MonoBehaviour {
    void OnMouseDown() {
        Application.LoadLevel("Scene1");
    }
}

And that's it! Skip to the next step if that is all you want. If you would rather call a separate method that loads the next level, look down a bit further before moving to the next step.

Now, I am going to make a method, called Play(), that will load the next level. This code will be activated when I click on the Game Object that holds this code. The OnMouseDown() method will simply trigger the Play() method, rather than actually loading the level itself.

using UnityEngine;
using System.Collections;

public class PlayButton : MonoBehaviour {
    void OnMouseDown() {
        Play();
    }
    void Play() {
        Application.LoadLevel("Scene1");
    }
}

Step Three: End

All you need to do now is to attach the script onto a Game Object that you would like to be the play button, in this case it will be Red Cube. Then Click the Play Button to activate the game and see if it works out. enter image description hereenter image description here

REMEMBER:

Make sure that you added the scene to the Build Settings, or else it will not work out properly. Search on Google to find out how to do that if you are not familiar with it.

References/More Info

Unity Script Reference: OnMouseDown()enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ I would STRONGLY recomend typing the script rather than copying and pasting this. This gives two benefits, one being you understand and memorise the code much more easily and quickly. Secondly, I could have made a typo mistake and as such simply copying and pasting could create an error. \$\endgroup\$ Jan 5, 2015 at 12:00
  • \$\begingroup\$ Problem solved :) \$\endgroup\$
    – zoran404
    Jan 5, 2015 at 12:57
  • \$\begingroup\$ I did it your way. But then I switched to the new GUI system \$\endgroup\$
    – zoran404
    Jan 6, 2015 at 6:19
  • \$\begingroup\$ Great @zoran404 :) \$\endgroup\$ Jan 6, 2015 at 10:07
0
\$\begingroup\$

What @Ahkam said is right 100% attach the Script to the gameobject you created and add a BoxCollider component to it ,then do the following in the script

using UnityEngine;
using System.Collections;

public class PlayButton : MonoBehaviour {
void OnMouseDown() {
    Application.LoadLevel("Scene1");
 }
}

Adding a BoxCollider component is Mandatory here for triggering the mouse event

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .