0
\$\begingroup\$

I have assigned this C# script to a spot light. But the green color which is at 2nd position in array never gets accessed and spot light never gets green light. I tried debugging this code but no idea what's wrong

public class CSharping : MonoBehaviour {

    public Light lt;

    Color[] allColor = { Color.red, Color.blue, Color.green}; 

    // Use this for initialization
    void Start () {
        lt  = GetComponent<Light>();
    }

    // Update is called once per frame
    void Update () {


        if(Input.GetButtonDown("Fire1"))
        {
            int colorVariable = Random.Range(0,2);
            lt.color = allColor[colorVariable]; 
        }

    }
}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Your problem is that when you call Random.Range(0,2), your only getting the output of 0 or 1. This, to me, sounds like reasonable behavior for the Random.Range() method. As has been explained to me, random is tricky for a computer, and the default method is very unreliable. \$\endgroup\$ – Gnemlock Oct 18 '16 at 8:12
  • \$\begingroup\$ @Gnemlock Random.Range(0,2)....here 0 and 2 are inclusive according to Unity docs \$\endgroup\$ – Abhimanyu Aryan Oct 18 '16 at 8:15
  • \$\begingroup\$ @Gnemlock please check my updated question \$\endgroup\$ – Abhimanyu Aryan Oct 18 '16 at 8:27
  • 2
    \$\begingroup\$ I'm actually in the process of answering, and your comment / update confirms it. You want to use Random.Range(0,3) \$\endgroup\$ – Gnemlock Oct 18 '16 at 8:28
1
\$\begingroup\$

You're looking at the another overload the Range method in the docs for float variables but using integers.

From Unity docs:

public static float Range(float min, float max);

Returns a random float number between and min [inclusive] and max [inclusive] (Read Only). Note that max is inclusive, so using Random.Range( 0.0f, 1.0f ) could return 1.0 as a value.

public static int Range(int min, int max);

Returns a random integer number between min [inclusive] and max [exclusive] (Read Only). Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9.

\$\endgroup\$
  • \$\begingroup\$ I have updated the question. Please check the screen shot from Unity Docs \$\endgroup\$ – Abhimanyu Aryan Oct 18 '16 at 8:27
  • \$\begingroup\$ this fixed the error but documentation is wrong \$\endgroup\$ – Abhimanyu Aryan Oct 18 '16 at 8:27
  • \$\begingroup\$ @AbhimanyuAryan, see my answer. I believe the error is in your interpretation. \$\endgroup\$ – Gnemlock Oct 18 '16 at 8:37
  • \$\begingroup\$ update answer, but @Gnemlock make it first =) \$\endgroup\$ – n4nn31355 Oct 18 '16 at 8:39
  • 1
    \$\begingroup\$ Seems if I was right, you could have just up voted my answer, instead of copying it. -1 \$\endgroup\$ – Gnemlock Dec 19 '16 at 22:55
6
\$\begingroup\$

You are using Random.Range(int, int), not Random.Range(float, float). In the example you give, the max value is not inclusive.

Let's take a look at the Unity API:

Random.Range

public static float Range(float min, float max);

Returns a random float number between and[sic] min [inclusive] and max [inclusive] (Read Only). Note that max is inclusive, so using Random.Range( 0.0f, 1.0f ) could return 1.0 as a value.

...

public static int Range(int min, int max); Returns a random integer number between min [inclusive] and max [exclusive] (Read Only). Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9. If max equals min, min will be returned.

- Unity API: Random.Range


Given that your returning an int, the method int Range(int min, int max) will be called, where max is exclusive. To return the values 0, 1 or 2 you should use Random.Range(0, 3).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.