How can I respond to a button click using GUI.Button?
I tried to post the following question on the community help forum but couldn't, can anyone here help me with this question?
I'm currently trying to add onClick methods to a button created at runtime. I have tried searching but can only find c# examples, could anyone help me or direct me as to how I would go about this using JavaScript, the following code is the button I have made.
GUI.Button(new Rect(280, 240, 50, 30), items[1]);
ps: I have tried using the c# examples but they just created errors so I haven't included my attempts.
Answer by Statement · Oct 31, 2015 at 03:06 PM
onClick exist on UnityEngine.UI.Button which is part of the new UI.
UnityEngine.GUI.Button is a static function which is part of the legacy IMGUI.
(Immediate Mode Graphical User Interface)
The two systems don't go hand in hand. It makes no sense to talk about adding event handlers to onClick on an IMGUI button because there exist none. You can write your own system on top of IMGUI if you want but I'd advise you to consider using the new UI if you can.
To handle a button click using IMGUI, read the return value of GUI.Button.
if (GUI.Button(rect, title))
{
// Button was clicked
}
Unity has video tutorials for the new UI, including Buttons.
Your answer
Follow this Question
Related Questions
Setting EventTrigger in Unity with JavaScript 0 Answers
How to create an event trigger script for intercepted events 0 Answers
Loading Game Crash 1 Answer
Multiplayer Hiding Layer just for Local Player 0 Answers
How can I change this java to C# 2 Answers