- Home /
Button UI, OnClick() - Calling Bool Function
So I've been working on a basic script that basically just confirms a decision that's made in a earlier in the Project (it's a chess game where you can forfeit the game, but I feel the decision needs a confirmation button to prevent costly misclicks). So, I thought I'd be smart and make it just a simple generic script that works for all my buttons (there's a few of them) to make life easier and to further myself in Coding in general. Here's my code:
Script that goes on the original button that starts the forfeit
public class UIOffer : MonoBehaviour
{
public GameObject confirm;
public UIConfirm confirmScript;
public void Clicked () //Makes decision
{
if(gameObject.layer == 9 && gameObject.tag == "Forfeit")
{
confirmScript = confirm.GetComponentInChildren<UIConfirm>();
confirm.active = true;
confirmScript.confirmForfeit();
if(confirmScript.confirmForfeit())
{
Manager.status = "CheckMate"; // Lose
}
}
}
}
Second script that confirms/denies the forfeit made in the first and returns it to the first
public class UIConfirm : MonoBehaviour
{
public bool confirmOffer() //Confirms decision
{
return true;
}
public bool confirmForfeit() //Cancels decision
{
return false;
}
}
My problem is that when I go into Unity, the functions aren't there under the 'OnClick()' component. I changed the "bool"s in the second script to "void", and the script appeared, and I could run the functions on the various buttons that I have - but they don't return a bool (obviously), which I do quite need. From this, I guessed that: either I've made a very stupid mistake (it would most definitely not be the first time), or Unity doesn't have a way to call a bool function off a UI button. Is there a way around it, or do I just need to try find a completely different way around this problem?
Any help is greatly appreciated, this has been 'bugging' me for a while, and it'd be great to get it out of the way :) Cheers in advance.
Having a shared set of functions can be beneficial to a bunch of things scattered around ad hoc, but in this case (going by your first IF statement) your conditions might end up making quite a mess.
If your goal is a confirmation window, you could try making a simple "Yes / No" window prompt through a coroutine. It might follow a pretty predictable flow:
create a simple window with two buttons (yes and no) and an event that fires when the window is closed by other means
create some way of keeping track what the user did here (maybe an enum)
construct that window with reference to component making the call (your UIOffer instance)
show the window
yield (wait) until one of those Yes or No buttons is clicked (or the window is closed)
After one of those things happens (or the window is closed), tell the UIOffer instance (that you supplied at the start) to check the prompt's value (yes, no, other)
If they clicked yes, forfeit.
If they did anything else, don't.
@TreyH Yeah, I've ended up changing my scripts slightly due to Daemonhahn's answer. Here's the updated version (although it's still not completely functioning properly).
public class UIOffer : $$anonymous$$onoBehaviour
{
public GameObject confirm;
public UIConfirm confirmScript;
public void Clicked () //$$anonymous$$akes decision
{
if(gameObject.layer == 9 && gameObject.tag == "Forfeit")
{
StartCoroutine(WaitUntilButton());
}
}
IEnumerator WaitUntilButton ()
{
confirmScript = confirm.GetComponentInChildren<UIConfirm>();
confirm.active = true;
UIConfirm.refresh = false;
while(UIConfirm.refresh == false)
{
yield return null;
}
UIConfirm.refresh = false;
if (confirmScript.confirm(confirmScript.x))
{
$$anonymous$$anager.status = "Check$$anonymous$$ate";
confirm.active = false;
}
else
{
confirm.active = false;
}
}
}
And the second script...
public class UIConfirm : $$anonymous$$onoBehaviour
{
public bool x;
static public bool refresh;
public void confirmOffer() //Confirms decision
{
refresh = true;
x = true;
confirm(x);
}
public bool confirm(bool x)
{
refresh = true;
return x;
}
public void cancel() //Cancels decision
{
refresh = true;
x = false;
confirm(x);
}
}
So I did end up changing it to a IEnumerator, because like you said, it makes it a lot easier.
Answer by Daemonhahn · Nov 01, 2016 at 04:48 PM
You can't return a bool from the on click methods, so instead write a void function that gives you the bool (I.e set the bool to what you need in the void and at the end of that void call the method that does the stuff or checks you need on that bool.
E.g
Void OnClickThing() { Bool x = true; BoolFunction(x); }
Or you could try and force unity to serialise it which may make it work
Cheers, this worked nicely, I should be able to do what I was trying to do now (hopefully). Thanks for the speedy reply :)
No problem, feel free to @ me anytime and I'll do my best to help, the Unity community is all about helping each other :)
Your answer
Follow this Question
Related Questions
How to make a custom onClick event? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Button only works once 1 Answer
Bool not changing on button press 1 Answer