- Home /
Generating a random number and detecting what number it is
Hey all, okay so the question is: I'm trying to get a random number in a certain range and then get that number and if certain number do certain thing.
BTW its in a multiplayer game and the function is for replacing the player:
private int playerchoice;
so thats the int im using :)
void Start()
{
playerchoice = Random.Range(1, 2);
if (isServer)
{
if (playerchoice == 1)
{
Server_ReplaceExplorer();
}
if (playerchoice == 2)
{
Server_ReplaceSolder();
}
}
}
Thanks in regards
Answer by SohailBukhari · Jul 20, 2017 at 06:48 AM
You are selecting number integer as you don't know when you call playerchoice = Random.Range(1, 2);
its means first number is inclusive
while second number is exclusive
so your statement always return 1.
playerchoice = Random.Range(inclusive, exclusive);
So in your code always execute playerchoice == 1
condition instead of playerchoice == 2
.
void Start()
{
playerchoice = Random.Range(1, 3);// select number between 1 and 3.
if (isServer)
{
if (playerchoice == 1)
{
Server_ReplaceExplorer();
}
if (playerchoice == 2)
{
Server_ReplaceSolder();
}
}
}
Select number between one and three so result will return either 1 or 2
.
Edit: you didn't tell anything about isServer
in your code just make sure your isServer
is true in Start()
Note: When you use float then both min and max are included.
It's usually better to think like a programmer and use 0-based indices. That way you just specify the count you want
playerchoice = Random.Range(0, 2);
2 means two possible results which are 0 and 1
Thank you! 2 means two possible results which are 0 and 1 good catch.
Answer by game4444 · Jul 20, 2017 at 06:39 AM
First of all check when your isServer bool is becoming true. Second is your random Random.Range(1, 2)
will always return 1. So if (playerchoice == 1) { Server_ReplaceExplorer(); }
would have to run. But in this case you have to check when isServer is becoming true and when false.
Answer by Kishotta · Jul 20, 2017 at 12:35 AM
Sounds like a job for a Switch statement, and possibly an Enumeration.
but why doesn't this work? i'm new to coding and just want to understand it a bit more.
I completely forgot. Random.Range (int $$anonymous$$, int max) uses an exclusive upper bound, unlike it's float counterpart.
For example, this code will only return values 0, 1, 2, 3, or 4.
Random.Range (0, 5);
Your serverReplace_explorer() function should always trigger with they way you are using the random number function. Is that happening?
Thanks for replying, Yea this is happening, but do you know why?
Answer by shadowpuppet · Jul 24, 2017 at 06:20 PM
I always put my randomRange stuff in the Awake function since it happens before Start so that number is determined already by the time the Start function kicks in. Maybe it doesn't make any difference as long as the randomRange thing is the first call in your start function, but I have had problems sometimes when I had them both in the start function.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer
Same Random.Range Value for 2 Objects. 2 Answers
If statement behaves unexpectedly 3 Answers