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 /
  • Help Room /
avatar image
0
Question by dead_byte_dawn · Aug 28, 2015 at 09:52 PM · coroutinerandom.range

Start and Stop with key input?

Hey Guys, what I'm trying to do here is create a very simple Lottery Simulator but, I'm stuck at getting the Update function to stop (not Pause) or stop when I reached the winning number. Any idea how I can get this working? I 'THINK' this needs to be a Coroutine but I'm just not sure which approach to take. Thanks!

What I have working:

  • Start displaying random numbers in GUI when the 'SPACE' key is pressed.

What I'm trying to figure out:

  • Stop the routine when the Winning int is found. OR...

  • Stop the routine when the 'SPACE' key is pressed a second time.

  • Slow down the update so it displays the numbers at are more readable pace( I know its deltaTime, just now positive how to implement it).

       public bool gameIsRunning = false;
         public Text Lottery;
         public int winNumber = 1;
     
         private int printRange;
     
         void Update() {
     
             StartupKey ();
     
             if (gameIsRunning) {
                 Lottery.text = "NUMBERS: " + Random.Range (1, 100).ToString ();
             } 
     
         }
     
     
     
         void StartupKey() {
             if (Input.GetKeyDown (KeyCode.Space))
             {
                 gameIsRunning = true;
             } 
    
    
    
    
    
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SpaceManDan · Aug 29, 2015 at 05:59 AM

Try this out? Should work just the way you asked.

 public bool gameIsRunning = false;
 public bool youWon = false;
 public Text Lottery;
 public int winNumber = 1;
 public int spinNumber;

 private int printRange;

 private float timerTime = 0.5f; //how long to wait until next roll.  Currently set to 0.5 seconds.
 private float timerNextTime;
 
 void Update()
 {

     StartupKey();

     if (gameIsRunning)
     {
         if (youWon == false)
         {
             if (timerNextTime <= Time.time)
             {
                 spinNumber = Random.Range(1, 100);
                 Lottery.text = "NUMBERS: " + spinNumber;
                 timerNextTime = Time.time + timerTime; //add timerTime to Time to make it wait until the next timer to check and show a score.
             }

             if (spinNumber == winNumber)
             {
                 youWon = true;
             }
         }

         if (youWon)
         {
             Lottery.text = "YOU WIN!!!";
         } 
     }
 }

 void StartupKey()
 {
     if (Input.GetKeyDown(KeyCode.Space) && gameIsRunning == false)
     {
         gameIsRunning = true;
     }
     else if (Input.GetKeyDown(KeyCode.Space) && gameIsRunning == true)
     {
         gameIsRunning = false;
     }
 }
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 SpaceManDan · Aug 29, 2015 at 06:01 AM 0
Share

Oh yeah, slowing it down.

add the code Time.timeScale = 0.5f;

1 is full speed. 0.5 is half speed... and so on.

avatar image SpaceManDan · Aug 29, 2015 at 06:11 AM 0
Share

O$$anonymous$$ never$$anonymous$$d about my timescale comment. It isn't helping. You will need to implement a Timer. Give me a second I'll help you out.

avatar image SpaceManDan · Aug 29, 2015 at 06:20 AM 0
Share

Ok, there. I edited the code to add a timer into it. That should do all the stuff you needed it to. You were on the right track. You just needed a few booleans to keep track of what state things are in. True or False is a switch. on and off. Very powerful in the right hands.

avatar image
0

Answer by dead_byte_dawn · Sep 01, 2015 at 08:07 PM

Thanks @SpaceManDan! I'll be checking your code out asap.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I generate a random number in regular time intervals? 0 Answers

Coroutine not working as expected 1 Answer

Execute coroutine in Update() 8 Answers

Using Grid-like singletons but have an issue with Coroutines. 2 Answers

MoveTowards and Lerp not working 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