- Home /
Returns 2 or more outputs, when I need only 1 output printed, by using Random.Range(...)?
Just to be not much complicated... I just want to print one random value between 0 to 3 on trigger enter... But I am getting somethings 1 or 2 outputs printed! I want just a single output to be printed, As I can use them in if statement
Here is my code...
C#
void OnTriggerEnter()
{
int randomNum = Random.Range(0, 4) ;
//As I want to get one of the 3 outputs
print(randomNum);
if (randomNum == 1)
{
// Do something....
}
if (randomNum == 2)
{
// Do something....
}
if (randomNum == 3)
{
// Do something....
}
}
I am getting out put in Unity Concole as: 2 and 3 Or sometimes 1 and 3 or just 1
Don't worry about that print I had done [string Num = randomNum.ToString();] and printed a value of [Num] as [print(Num);] The only problem is I am getting Multiple Outputs, I think you get what I mean Plz help to over come this... Thank you!
Answer by Sidd_Code · Jun 16, 2020 at 04:49 PM
Ok i got it I find The Answer by my self
Just Write The line of code inside the start function: void Start() { int randomNum = Random.Range(1, 4); }
And put the if Statements wherever you want inside the code, and then you will get a single out put (actully i figured out where the problem was, But it is littil bit complex to explain... so let it be.)
Your answer
Follow this Question
Related Questions
Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer
OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 1 Answer
on / off if statement C# 1 Answer
Hey! Im a beginner and need help in randomizing. (Im making a 2d game in Unity) 1 Answer
Hide the Object Picker in Unity Editor in ObjectField 2 Answers