Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Random Range number every 5 seconds

Discussion in 'Scripting' started by JohnDmu, Apr 19, 2015.

  1. JohnDmu

    JohnDmu

    Joined:
    Apr 8, 2015
    Posts:
    33
    Hello! Lets say i have this code to generate a random number from an empty game object .


    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.      static GameManager m_instance = null;
    4.      public static GameManager Instance
    5.      {
    6.           set
    7.           {
    8.                if (m_instance == null)
    9.                {
    10.                     m_instance = value;
    11.                }
    12.                else
    13.                {
    14.                     Debug.LogError("GameManager: You can't have more than 1 GameManager at a time!");
    15.                }
    16.           }
    17.           get
    18.           {
    19.                return m_instance;
    20.           }
    21.      }
    22.      public minimum = 1f;
    23.      public maximum = 3f;
    24.      public float moveFactor = 0f;
    25.      void Start()
    26.      {
    27.           Instance = this;
    28.      
    29.           moveFactor = Random.Range(minimum, maximum);
    30.      }
    31. }


    After this
    i use the same generated number in two obstacles
    by calling it like this !

    Code (CSharp):
    1. float value = GameManager.Instance.moveFactor;
    But my problem is that only one number is generated and my next obstacles are using the same numbers.!
    how can i have an instance that generates a number in a range every 5 seconds?

    and will i be able to call it the same way ??
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Use a coroutine, something like


    Code (CSharp):
    1. IEnumerator NumberGen(){
    2.         while(true){
    3.                 moveFactor = Random.Range(minimum,maximum);
    4.                 yield return new WatForSeconds(5);
    5.         }
    6. }
    7.  
    8. //Put this in Start()
    9.  
    10. StartCoroutine(NumberGen());
    You should probably use a custom boolean instead of just while(true), so you can stop the random number generation when not needed.
     
  3. JohnDmu

    JohnDmu

    Joined:
    Apr 8, 2015
    Posts:
    33
    hmm thanks!! give me a sec to give it a try!!!
     
  4. JohnDmu

    JohnDmu

    Joined:
    Apr 8, 2015
    Posts:
    33
    Without a custom boolean this will run forever right???

    YOU ARE AWESOME MAN...saved my life...
     
  5. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Yes, without a boolean, unless you use StopAllCoroutines() (which I'm pretty sure isn't a good thing to use), it will run until the monobehaviour is disabled or destroyed.

    And no problem.
     
  6. JohnDmu

    JohnDmu

    Joined:
    Apr 8, 2015
    Posts:
    33
    Have a nice day!! I was stuck on this for 3 days now and never thought of a coroutine.!
    so simple !!! thank you!!!