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
0
Question by TakaraLay · Jul 17, 2016 at 08:32 PM · buttonfunction

How do I allow my function to run only once at a time?

So in my program I have a button that when I press will generate a series of random numbers and output it into the UI. I got my random number generator working but I could not figure out a way to not let the the function run only one at a time.

I need it so that when I press the button (Roll()), the button will not be interactable for the time the numbers are being generated, then when the function finishes performing, the button becomes interactable again.

The problem for me now is I don't know while my while statement is not making the button not interactable and so I can press my button many times and the function will just run multiple times at a time cause lots of random numbers to be displayed which is what I don't want.

Thank you for any help in advance.

 private int RanNum;
 private bool Rolling = false;
 
 public Text RolledNum;
 public Text HasBeenRolled;
 public Transform RollButton;
 
 void Start () {
     RollButton.GetComponent<Button> ().interactable = true;
 }
 
 void NumRoll(){
     RanNum = Random.Range (1, 15);
     RolledNum.text = RanNum.ToString ();
 }
 
 void RepeatRoll(){
     InvokeRepeating ("NumRoll", 0, 0.3f);
     Rolling = true;
     while (Rolling == true) {
         RollButton.GetComponent<Button> ().interactable = false;
     }
 }
 
 public void Roll(){
     RepeatRoll ();
 }
 
Comment
Add comment
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

1 Reply

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

Answer by MrRightclick · Jul 18, 2016 at 01:36 PM

You could use a coroutine and let it run in a while(true) loop until you have rolled all the numbers, or then just put a simple timer function on the button like so (untested code, but you get the gist):

 public void ButtonPressed () {
     button.interactable = false;
     Invoke("ReActivateButton", 2);
 }

 public void ReActivateButton () {
      button.interactable = true;
 }
Comment
Add comment · Show 2 · 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 TakaraLay · Jul 18, 2016 at 06:00 PM 0
Share

Hey, thank you so much! It's doing what I want it to now but I'm not sure if it's the most efficient way but anyways it works! I still don't know why my previous method does not though... weird. Thank you!

avatar image MrRightclick TakaraLay · Jul 19, 2016 at 10:30 AM 0
Share

Glad it worked. The only thing I'd be worried about (if the randomization could take time depending on the platform) is the button beco$$anonymous$$g active before the timer runs out.

You should also check coroutines if you aren't familiar with them yet. Very useful, very flexible, and I think they would work wonders here: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changes in variables from function triggered by editor script during edit mode are not applied. 2 Answers

Jumping on moving platforms problem c# 1 Answer

OnSelect and OnPointerEnter work in one script but not another on the same object 2 Answers

The variable has not been assigned - but it has 1 Answer

Create GUI based on an array 2 Answers


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