Search code examples
c#unity-game-engineunity-networking

Random Number Same on both Client and Server


I want a random number to be same on client and server. I'm doing that with Random.Range(min,max)

but the problem is its giving me random number different on both server and client which is messing up my game.

I've tried [SyncVar] for the variable and [ServerCallBack] for the method (which only returns a random number)

But both are not working

Can someone please help ?

Code:

[ServerCallback]
int NewRandom(int maxLimit)
{
    //if (this.transform.parent.GetComponent<NetPlayerCheck>().isPlayer1) {
    //if (Network.isServer) { 
    return Random.Range(0, maxLimit);
    //}
}

[SyncVar] int rand = newRandom(5);

Solution

  • If both server and client randoms a number ofcourse they will be differeent. Why not get a random number from server side and pass to client if both needs to be same?