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

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:
    4
    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:
    7,150
    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:
    289
    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:
    4
    These values change with time
     
  5. Brathnann

    Brathnann

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