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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using Random.Range() not working

Discussion in 'Scripting' started by Shadowing, Jul 21, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    I think I'm going crazy here guys.

    This always logs 0
    Code (csharp):
    1.  
    2.  int RandomRange = Random.Range (0, 5);
    3.  Debug.Log (RandomRange);
    4.  
    This always logs 4
    Code (csharp):
    1.  
    2.  int RandomRange = Random.Range (1, 5);
    3.  Debug.Log (RandomRange);
    4.  
     
  2. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Issue seems to have something to do with running Random.Range() inside the Start() function.
    If I run it inside Update() or Awake() works fine
     
  3. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Race condition with RandomRange being seeded perhaps, if you need to use it there you could seed it yourself using the system time or something.
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Ya that's the only thing I can think of. Using it in Awake is ok in this situation. Just going to do that. Thanks for the reply Strategos
     
  5. typane

    typane

    Joined:
    Nov 25, 2011
    Posts:
    297
    I ran into the same issue when trying to run Random.Range off inherited static classes, essentially Start would superseed the base classes and basically just become nonsensical, in the end i had to call it from a game manager to populate the fields.