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

Random.range does not work

Discussion in 'Scripting' started by BT723, May 18, 2018.

  1. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    I am not sure how to randomise numbers in unity and random.range is not in auto complete and does not work
    here is the script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class randomnumb: MonoBehaviour {

    public int locations;

    public void RandomGenerate()
    {
    locations = Random.Range(1, 4);
    }
    void Update () {
    if (locations == 1)
    {
    transform.position = new Vector3(1063, 52, 2290);

    }
    if (locations == 2)
    {
    transform.position = new Vector3(1063, 89, 2140);

    }
    if (locations == 3)
    {
    transform.position = new Vector3(1063, 111, 1766);

    }

    }
    }
     

    Attached Files:

  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    If that's all of your script, then you're never calling RandomGenerate(), so `locations` will always have a value of 0. Did you intend to call RandomGenerate() in the Start method, so that the object has a random position when the scene loads? Or did you mean to call it at some other time?
     
  3. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    I meant to call it when it starts
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Does that mean you're all set now, and it's working as expected?

    Code (CSharp):
    1. void Start() {
    2.     RandomGenerate();
    3. }
    In general, if code doesn't seem to be doing what you expect, you'll need to learn to debug it. The best approach is to attach the Visual Studio debugger, and set breakpoints in the code. You'll eventually want to learn to do that. If that seems complex, you can add `Debug.Log()` statements to your code, so then you can see what log messages are getting written when your code runs. For example:

    Code (CSharp):
    1.  
    2.     void Start() {
    3.         RandomGenerate();
    4.         Debug.Log(string.Format("Location is: {0}", locations));
    5.     }
    6. }
    One other thing: You probably don't need to call your position changing code in Update(). Most of the time it's not going to change the object's position. So, you're probably better off just putting that code in your RandomGenerate() method.
     
    Doug_B likes this.
  5. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    In visual studio random.range does not show up in the auto complete this is the same with debug im not sure if this is a problem with visual studio or not.
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    I'm not really sure what would cause that. Make sure you're using the right casing. For example, it's Random.Range, not random.range. You also need to be importing UnityEngine, but it looks like you were already doing that. If you're not getting any autocompletion for any Unity classes, I'd try reinstalling Unity.
     
  7. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    I reinstalled unity and Random.range is in auto complete along with debug.log but the script is still not changing the location of the object
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What about this?
    Code (csharp):
    1. public class randomnumb: MonoBehaviour {
    2.  
    3. public int locations;
    4.  
    5. void Start()
    6. {
    7.    RandomGenerate();
    8. }
    9. public void RandomGenerate()
    10. {
    11.    locations = Random.Range(1, 4);
    12.    Vector3 pos = Vector3.zero;
    13.  
    14.    switch(locations)
    15.    {
    16.       case 1: pos = new Vector3(10,2,0);
    17.               break;
    18.       case 2: pos = new Vector3(-3,-2,0);
    19.               break;
    20.       case 3: pos = new Vector3(0,3,0);
    21.               break;
    22.    }
    23.  
    24.    transform.position = pos;
    25. }
    26.  
    27. void Update()
    28. {
    29.    if(Input.GetMouseButtonDown(0)) RandomGenerate();
    30. }
    31. }
    Put that on the game object you want to move, hit play and then click the mouse button to get a new spot?
    :)
     
  9. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    that does not seem to work thanks anyway
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    The code is working, so there must be something else that's not setup correctly, or some other code affecting the game object, perhaps?
     
  11. N00MKRAD

    N00MKRAD

    Joined:
    Dec 31, 2013
    Posts:
    210
    https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    @BT723 Hopefully you know these scripts have to be on gameobjects in the scene and that gameobject needs to be active.

    Your best bet is to add Debug.Log("comment here"); in each method. Add one to Start, GenerateRandom, and Update and make sure your methods are running. If you don't see the logs, then you have something hooked in wrong.
     
  13. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    when you say the game object needs to be active what do you mean?
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Neither the game object nor the script can be disabled.
     
  15. BT723

    BT723

    Joined:
    Apr 11, 2018
    Posts:
    30
    Thank you to all it has suddenly started working