- Home /
How to add event to an object
Hi, I'm trying to create an 2D game in unity and I have some problems. I want to create the menu of the game, and I created the buttons as images to add scripts to each button to do something. I've created this script to the button "Start Game":
using System.Collections;
public class Jogo : MonoBehaviour
{
public string scene;
private bool loadLock=false;
void Update ()
{
if (Input.GetMouseButtonDown (0) && !loadLock)
{
LoadScene();
}
}
void LoadScene()
{
if (scene == null)
return;
loadLock = true;
Application.LoadLevel (scene);
}
}
And I add this script to the button, but this script is not only executed when I clicked the button but is executed anywhere I click. How I resolve this?
Answer by robertbu · May 15, 2014 at 05:30 PM
If your images are in world space (i.e. not GUITexture or GUI.DrawTexture images), then use OnMouseDown().
Click the checkbox / Accept this Answer icon over there below the thumbs
<------
Your answer
Follow this Question
Related Questions
2D game movement/jump issue 2 Answers
How do you create a menu using C# in Unity? 1 Answer
Disabling the Z axis? 1 Answer
Loadlevel and buttons... 2 Answers