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 :slight_smile:

    void Start()
    {
        playerchoice = Random.Range(1, 2);

        if (isServer)
        {
            if (playerchoice == 1)
            {
                Server_ReplaceExplorer();

            }
            if (playerchoice == 2)
            {
                Server_ReplaceSolder();

            }
        }
    }

Thanks in regards

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.

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.

Sounds like a job for a Switch statement, and possibly an Enumeration.

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.