Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

I want some help with having some Randome Values

Discussion in 'Scripting' started by Lotfi13, Jun 14, 2018.

  1. Lotfi13

    Lotfi13

    Joined:
    Jun 14, 2018
    Posts:
    2
    Hello everybody
    I want to know how we can have a random value between known values
    ex:
    I want a random value between (1,3,8,6)
    and I do not want him to return any other value than these four
    so Random.Range is not valid
    and thank you
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,976
    Add the numbers to a list/array.
    Then do a random.range to pick an index from that list or array
    Return the value at that index.
     
    Doug_B and whileBreak like this.
  3. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    287
    Code (CSharp):
    1. int[] numbers = new int[4]{1,3,8,6};
    2.  
    3.  
    4. ...
    5. int rndNumber = numbers[Random.Range(0,4)];
     
  4. Lotfi13

    Lotfi13

    Joined:
    Jun 14, 2018
    Posts:
    2
    These values change with time
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,976
    You can just redo the list than. Add or remove or start it fresh when you need to.
     
    whileBreak likes this.