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 HellsPlumber · May 17, 2019 at 01:29 AM · randommath

Random InitState not working with Coroutines / IEnumerator?

Hi!

When you first start our game, world objects such as houses are instantiated in random positions using a given seed.
The idea was to save the seed, then use Random.InitState(seed) to re-generate the same layout the next time the game is played.

The issue is that it doesn't seem to work with coroutines.

If I use InitState via FixedUpdate it works as expected, but our prefab instantiation is done via coroutines to prevent the game "freezing" while everything is placed down.

Here's a quick example script to show the issue.
This code SHOULD give the same 4 numbers each time you click "Run".
But if you click "Run" a bunch of times and you'll start to inconsistent results.

 using System.Collections;
 using UnityEngine;
 
 public class randomTest: MonoBehaviour
 {
 public bool run = false;
 public IEnumerator RandomNumbers()
     {
         int iteration = 0;
         int xIterations = 4;
         Random.InitState(255);
         for (int x = 0; x < xIterations; x++)
         {
             iteration++; 
             Debug.Log(iteration.ToString()+" = "+Random.Range(0,100));
         }
     }
 
     private void FixedUpdate()
     {
         if(run)
         {
             run = false;
             StartCoroutine("RandomNumbers");
         }
     }
 }



I've been struggling with this all day. It SHOULD work but simply doesn't.
My best bet is something to do with timing, but even adding a WaitForFixedUpdate or WaitForEndOfFrame doesn't seem to help.

Please let me know if you can help!

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
0
Best Answer

Answer by HellsPlumber · May 17, 2019 at 06:18 PM

I'm not sure why, but removing "yield return null" seems to fix this issue.

I have no idea if this is a glitch with coroutines, a timing issue or something else but I guess my issue is solved.
Sadly the whole point of yield was to prevent game "freezes" while instantiating multiple game objects, but I'll sacrifice it for consistent results.

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
avatar image
0

Answer by CoryButler · May 17, 2019 at 01:44 PM

@HellsPlumber

You need to prevent the Coroutine from running if it's already running.

Try adding an _isRunning toggle.

     public bool run = false;
     private bool _isRunning = false;
 
     public IEnumerator RandomNumbers()
     {
         int iteration = 0;
         int xIterations = 4;
         Random.InitState(255);
         for (int x = 0; x < xIterations; x++)
         {
             iteration++; 
             Debug.Log(iteration.ToString()+" = "+Random.Range(0,100));
             yield return null;
         }
         _isRunning = false;
     }
  
     private void FixedUpdate()
     {
         if(run && !_isRunning)
         {
             run = false;
             _isRunning = true;
             StartCoroutine("RandomNumbers");
         }
     }
Comment
Add comment · Show 1 · 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 HellsPlumber · May 17, 2019 at 05:15 PM 0
Share

I tried the code you pasted but I'm still having the issue as you can see here:
alt text



As you can see, it's not super-common but it's enough to make things entirely unreliable.

annotation-2019-05-17-181354.png (23.0 kB)

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

118 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

Related Questions

Random.seed Repeating 2 Answers

How to make Random.Range() step by a certain amount? 1 Answer

Random.rotationUniform clarification 4 Answers

Problem in math or Random in flash export? 3 Answers

Algorithm for number generation that is random but consistent 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