- Home /
How to make a button work?
Well, I have a main menu scene, three buttons, all of them are textures imported to the engine, all of them with it's own collision box. But I want to define that when I click at X button's collision box, we are going to the scene Y. How can I do that?
Hello, you make a action on button click.
if (GUI.Button(Rect(10,70,50,30),"Click"))
{
Application.LoadLevel ("Y");
}
Answer by Hexer · Jan 06, 2015 at 10:38 AM
You can also use OnMouseDown.
function OnMouseDown()
{
Application.LoadLevel("Y");
Debug.Log("Load Y");
}
I prefer this method over using GUI.Button because with this method I can use any object or sprite as a button. Just be sure to add a collider to your object. For sprites you want to use BoxCollider2d or PolygonCollider2d.
Add this script to object "X"
But How can I get these four cordinates of the rectangle?
The collider you put in the game object will handle the coordinates, you don't have anything more to do after that.
I'e used the way you said, and it works, but what I wanted to do is, I have the textures of the buttons placed on the scene in their right place with their right size, I wanted to make a scene change when I click their position or their collision box, How could I do that?
You create a script containing this function On$$anonymous$$ouseDown, you attach it to the game object that contains the button texture and a collider, and it is over.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
help assinging a touch button to left click 0 Answers
Scene Change Collision 2 Answers
Analysing player metrics help 0 Answers