- Home /
Having issues determining a way to use buttons in a script.
Hi, first, I do know that you can use On Click() and add in functions and various things, but to me that seems very complicated and just not programmer friendly... atleast the way I'm looking at it.
So I've tried a couple of things but I cant seem to find the right function for what I'm trying to do.
Basically I want something I can use to trigger an if statement, when I press a button down. Currently I've tried OnPointerDown, but it seems that it is an event and not a Bool value, and unfortunately I do not understand the event system. In anycase if anyone could help in giving me an idea of how I can do this that would be awesome!
Also worth noting button is not noticed as a component in declaration unless declared as seen below, not sure if thats a bug... IE "Button Quit;" I imagine the code would look something like this.
using UnityEngine;
using System.Collections;
public class quit : Monobehaviour{
public UnityEngine.UI.Button Quit;
void start(){
Quit = GetComponent<UnityEngine.UI.Button>();
}
void update(){
if (quit.MAGICFUNCTIONRELATEDTOCLICKING == true){
Debug.Log ("Application was exited");
Application.Quit();
}
}
}
Answer by CastleIsGreat · Feb 25, 2015 at 05:28 AM
I ended up writing a function to handle it using the event system, though I definitely was not asking for information on that. I'd still like another way to do it by using the button to flip a bool value for ifstatements. In anycase, I'm using sendmessage to call functions instead...
Answer by tanoshimi · Feb 24, 2015 at 07:30 AM
Rather than guessing, I recommend you follow some tutorials about how to use the UI system: http://unity3d.com/learn/tutorials/modules/beginner/ui
Your answer
Follow this Question
Related Questions
Canvas button with 2 functions onClick must be pressed twice for 2nd function? Will not disappear? 1 Answer
How to ADD one Buttons.onClick events to another Buttons.onClick? 1 Answer
Show UI Button press color from script when invoking onClick 2 Answers
Looping through Button Array and checking for OnClick 0 Answers