Detecting Button Click
How do i let my game react if i press on a button
Which button where? On the UGUI layer? A 3D button in game that you mouse click on? A button on your keyboard? An xbox controller button? A chocolate button from a packet you just found wedged down the side of the sofa?
Give us some more information.
Unity provides nice tutorials about scripting, you will find your solution here :
http://unity3d.com/learn/tutorials/topics/scripting/getbutton-and-getkey?playlist=17117
Answer by Mmmpies · Jun 07, 2016 at 05:13 PM
Alright I'll stop joking! This is really easy and more importantly really easy to search for. Watch some of the tutorials for the UI but this is the basics.
Create a script (I'll use C# for this) and in that script create a public void function. It HAS to be PUBLIC so like this
public void PushTheButton()
{
print("You pressed my button");
}
leave Start() and Update() and the rest of your script as is just add that function. Save and drag the script onto the button. Then click on the button and look in the inspector.
Click + in the OnClick area of the button and a slot appears, drag your button onto the slot (even though the slot is on the button!). From the drop down select your script name -> PushTheButton.
And that's it, if you click on the button now it'll call that function and do whatever is in it, in this case send "You pressed my Button" to the console.
Follow this Question
Related Questions
XCode Linking Errors. Unity 5.2.3f1 XCode 7.2 1 Answer
Instatiated object not being referenced in Start function? 2 Answers
Hello How can I Save Player position and a Gameobject which is a UI Text? 0 Answers
Showing a UI canvas when player enters a specific location 2 Answers
Draw snapping lines 0 Answers