Search Unity

  1. Unity 2018.3 is now released.
    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. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

I want some help with having some Randome Values

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

  1. LGDCompany

    LGDCompany

    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:
    4,095
    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. LGDCompany

    LGDCompany

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

    Brathnann

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