# Calling Random.Range

Hi guys, I’m new to coding but I have a very simple code that I made but having trouble calling it in an ‘if’ statement. It’s strange, the hit2 sound plays but the others do not, the random number works when called from debug.log just not sure what I’m doing wrong. The sounds do not work if their set to 3D, they only work if their 2D and I’ve converted all hit sounds to 2D. Here is the code, thanks for any advice -

using UnityEngine;
using System.Collections;

public class Sound_Player : MonoBehaviour
{

``````private int randomNumber;
public AudioClip hit1;
public AudioClip hit2;
public AudioClip hit3;

// Use this for initialization
void Start ()
{
randomNumber = Random.Range (0, 5);
if (randomNumber <= 1)
{
AudioSource.PlayClipAtPoint(hit1, Camera.main.transform.position);
if (randomNumber <= 3)
{
AudioSource.PlayClipAtPoint(hit2, Camera.main.transform.position);
}
else
{
AudioSource.PlayClipAtPoint(hit3, Camera.main.transform.position);
}
}

}

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

}
``````

}

So, when you call Random.Range(0,5) it generates a single number between 0 and 5, it is not an alias for calling Random.Range(). So on line 9, randomNumber gets set to something…say `0.15f`

Then you check to see if it’s less than or equal to 1. (it is)

Then you check to see if it’s less than equal to 3, which it still is.

So line 15 is executed.

Line 18 will never be executed because there is nothing that is less than 1 and also greater than 3.

To fix it, add `randomNumber = Random.Range (0, 5);` at line 13, before the if statement
That way, your logic is “Generate a random number, if its less than 1, do something.”

dosomething: " generate another random number, if it’s less than 3, do something else, if not do the other thing"

EDIT 1: The way I understood your logic is that it has a 1 in 5 chance that hit1 is called and then theres a 3/5 chance that hit2 will get called OR a 2/5 chance hit3 will get called. If you logic is that you want 2/6 chance of hit1, or a 2/6 chance of hit2 or a 2/6 chance of hit3 then @lazdena’s answer is correct. (notice its’ 2/6 since 0-1 map to hit1, 1-3 maps to hit2, and 3-5 maps to hit3)

This is a good situation for a switch case.

``````int number = Random.Rand(0,2);
switch (number)
{
case 0:
AudioSource.PlayClipAtPoint(hit1, Camera.main.transform.position);
break;
case 1:
AudioSource.PlayClipAtPoint(hit2, Camera.main.transform.position);
break;
case 2:
AudioSource.PlayClipAtPoint(hit3, Camera.main.transform.position);
break;
}
``````

If you’re new to programming, switch cases are good for when you are trying to check if one variable is equal to something else and want to handle every possible case that it could be. It’s the same as doing…

``````if (number == 0)
// Play Sound
if (number == 1)
// Play Sound
if (number == 2)
// Play Sound
``````

Thanks guys, I read through and found the error, I would still be scratching my head if it were not for these forums This is my new code that works!

``````using UnityEngine;
using System.Collections;

public class Sound_Player : MonoBehaviour
{

private int randomNumber;
public AudioClip hit1;
public AudioClip hit2;
public AudioClip hit3;

// Use this for initialization
void Start ()
{
randomNumber = Random.Range (0, 5);
if (randomNumber <= 1)
{
AudioSource.PlayClipAtPoint (hit1, Camera.main.transform.position);
}
else if (randomNumber <= 3)
{
AudioSource.PlayClipAtPoint (hit2, Camera.main.transform.position);
}
else
{
AudioSource.PlayClipAtPoint (hit3, Camera.main.transform.position);
}

Debug.Log (randomNumber);
}

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

}
}
``````