Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 daylerees · Jun 15, 2020 at 09:35 AM · random.rangetimers

How to use a random integer from range inside a timer?

Hi guys, web dev here trying Unity for the first time.

I've created a System.Timer from Start() with an interval of one second, which I've confirmed is executing the callback using a log, here's the setup:

     timer = new Timer(1000);
     timer.Elapsed += Tick;
     timer.Start();

Here's the callback:

 void Tick(object sender, ElapsedEventArgs e)
 {
     Debug.Log("timer tick");
     int rnd = Random.Range(1, 359);
     Debug.Log(rnd);
 }

The output I'm getting shows the timer tick message, but nothing else. No code I place after the range method is working. The range method works fine in constructor methods for example, but not within this timer callback, am I missing something? Thanks!

Comment
Add comment · Show 1
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 Maldonad1o · Jun 15, 2020 at 11:25 AM 0
Share

I am also looking for the same to produce random integers in a given range $$anonymous$$yCFAVisit.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TomArano_Dimenco · Jun 15, 2020 at 10:33 AM

Dont exactly know why this is happening.. however it can be fixed using the "System.Random" instead of "UnityEngine.Random".

         Debug.Log("timer tick");
         System.Random rand = new System.Random();
         int rnd = rand.Next(1, 359);
         Debug.Log(rnd);



Also, seems timers wil not stop executing when stopping unity playback (this might cause problems later).

As you are new to Unity, unity has its own 'timer' system we usually use coroutines to do stuff like this

     void Start()
     {
         StartCoroutine(triggerRandomNumber());
     }
 
     IEnumerator triggerRandomNumber()
     {
         while (true) //this is allowed in coroutines when yielding with delay (waitforseconds)
         {
             yield return new WaitForSeconds(1);
             Debug.Log("timer tick");
             int rnd = UnityEngine.Random.Range(1, 359);
             Debug.Log(rnd);
         }
     }

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 drees_unity · Jun 15, 2020 at 10:38 AM 0
Share

Thanks $$anonymous$$! In the end, I simply used Time.time in the update function to roll-my-own timers. Good to know about Coroutines. I use Goland in my work day-to-day so you'd think I'd have considered that. :),Hi $$anonymous$$!

Thanks for your help. I hadn't considered a Coroutine (funny, since my work is mostly in Golang!) but in the end, I simply used Time.time in the update function to create my own ti$$anonymous$$g methods. This seems to work, good to know I wasn't making an obvious mistake, though, seems like it could be a bug.

Thanks!

avatar image TomArano_Dimenco drees_unity · Jun 16, 2020 at 09:22 AM 0
Share

not sure if its bugged or intended behaviour. might be intended since timer keeps running out of play mode u cant use unity functions in there.

https://docs.unity3d.com/ScriptReference/Time-deltaTime.html $$anonymous$$ight be worth checking out aswell (since u are new with unity).. works great for making time based movement/functions.

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

130 People are following this question.

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

Related Questions

Finding diastance between two random coordinates..... 2 Answers

Returns 2 or more outputs, when I need only 1 output printed, by using Random.Range(...)? 1 Answer

A couple of questions about Networking. 0 Answers

Need help with enemy stop and go movement 2 Answers

Start ambient audio tracks at random points? 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