Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unable to get Random.Range(0, 4) to work

Discussion in 'Scripting' started by joe_04_04, Nov 11, 2017.

  1. joe_04_04

    joe_04_04

    Joined:
    Sep 15, 2017
    Posts:
    14
    I'm not able to get Random.Range(0, 4) to work. This is how I'm using it:

    int number = Random.Range(0, 4);

    I've read most of the answers currently on the unity forums and nothing seems to work, Random.Range(0, 4) is always returning a 0 or a 1.

    The weirdest part of this all is that I've used Random.Range() before with no issue and I didn't have a different syntax at all.

    I've tried changing my int number to float number, I've initialized number to zero before assigning a Random.Range() value to it. I have no idea why it won't return anything other than 0 or 1 (and those are almost 100 percent alternating each time).

    Any help would be greatly appreciated.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Do you see any error message in the Console window? Perhaps it's an ambiguity between System.Random and UnityEngine.Random?
     
  3. joe_04_04

    joe_04_04

    Joined:
    Sep 15, 2017
    Posts:
    14
    Last edited: Nov 11, 2017
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589