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. Dismiss Notice

Random Range Issues

Discussion in 'Scripting' started by Will-Metcher, Dec 13, 2014.

  1. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    Hey Guys
    I am trying to make this script which chooses a random number between 1 and 10(inclusive) and then assign a string to a different variable, but when i run the game it just defaults the number to 0 and won't change back.

    #pragma strict

    /*Names */
    public var B1 = "Bob";
    public var B2 = "Tim";
    public var B3 = "Mike";
    public var B4 = "Dale";
    public var B5 = "John";
    public var G1 = "Mia";
    public var G2 = "Zara";
    public var G3 = "Ava";
    public var G4 = "Evie";
    public var G5 = "Savannah";

    /*Shop Items*/
    public var Clock = 3;
    public var Books = 8;
    public var ToyCar = 6;
    public var ToyForkLift = 3;
    public var SandTimers = 2;
    public var WoodenDish = 4;
    public var PottedPlant = 7;
    public var WhiteBoard = 1;
    public var Vase = 4;
    public var Wrench = 3;
    public var CrowBar = 3;
    public var SledgeHammer = 2;
    public var Soap = 6;
    public var AlarmClock = 4;
    public var SilverTray = 5;

    var PersonChoice = Random.Range(0f,11f);
    var CurrentPerson = "";

    function Update () {
    if(PersonChoice > 1 && PersonChoice < 2)
    {
    CurrentPerson = B1;
    }

    else if(PersonChoice > 2 && PersonChoice < 3)
    {
    CurrentPerson = B2;
    }

    else if(PersonChoice > 3 && PersonChoice < 4)
    {
    CurrentPerson = B3;
    }

    else if(PersonChoice > 4 && PersonChoice < 5)
    {
    CurrentPerson = B4;
    }

    else if(PersonChoice > 5 && PersonChoice < 6)
    {
    CurrentPerson = B5;
    }

    else if(PersonChoice > 6 && PersonChoice < 7)
    {
    CurrentPerson = G1;
    }

    else if(PersonChoice > 7 && PersonChoice < 8)
    {
    CurrentPerson = G2;
    }

    else if(PersonChoice > 8 && PersonChoice < 9)
    {
    CurrentPerson = G3;
    }

    else if(PersonChoice > 9 && PersonChoice < 10)
    {
    CurrentPerson = G4;
    }

    else if(PersonChoice > 10 && PersonChoice < 11)
    {
    CurrentPerson = G5;
    }

    Debug.Log(CurrentPerson);

    }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    "Range can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function."


    The error is pretty clear about what you need to do

    Id also suggest you change your approach to using arrays
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. /*Names */
    4.  
    5. public var Names : String[] =[
    6. "Bob",
    7. "Tim",
    8. "Mike",
    9. "Dale",
    10. "John",
    11. "Mia",
    12. "Zara",
    13. "Ava",
    14. "Evie",
    15. "Savannah"
    16. ];
    17.  
    18.  
    19. var PersonChoice = 0;
    20. var CurrentPerson  = "";
    21.  
    22. function Update () {
    23.     PersonChoice = Random.Range(0,10);
    24.     CurrentPerson = Names[PersonChoice];
    25.     Debug.Log(CurrentPerson);
    26. }
     
    Stoven likes this.
  3. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Also
    Code (csharp):
    1. if(PersonChoice > 1 && PersonChoice < 2)
    greater than 1 and less than 2 will never be true....
     
    Stoven likes this.
  4. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    1.5? The OP is using the float variation of Random.Range (although quite why I'm not sure!)
     
  5. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    Yeah probably did not even have to post this, I got Frustrated yesterday and was not thinking straight. came back this morning and fixed it but thanks anyway