Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Gooey100 · Nov 01, 2016 at 03:22 PM · c#uiboolonclick

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Nov 01, 2016 at 06:07 PM 1
Share

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.

avatar image Gooey100 TreyH · Nov 01, 2016 at 06:21 PM 1
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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); }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daemonhahn · Nov 01, 2016 at 04:49 PM 0
Share

Or you could try and force unity to serialise it which may make it work

avatar image Gooey100 · Nov 01, 2016 at 06:11 PM 1
Share

Cheers, this worked nicely, I should be able to do what I was trying to do now (hopefully). Thanks for the speedy reply :)

avatar image Daemonhahn · Nov 01, 2016 at 08:19 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges