Search Unity

Question Random number not syncing for clients

Discussion in 'Relay' started by ItsKody, Jan 2, 2023.

  1. ItsKody

    ItsKody

    Joined:
    Jun 11, 2019
    Posts:
    4
    Hello,

    I have been stuck on this issue for a few days. I feel like I have tried a million different things and in the end it still is not syncing properly.

    The problem is I am trying to spawn a random gameobject on trigger. So far it works. It spawns a random object from the list of objects i have in the NetworkManager gameobect.

    I am using Netcode and Relay. The problem is when i initialize a random value to be loaded it gives each client a different random number so its not loading the same gameobject. HOW DO I SYNC RANDOM NUMBERS?? I am so stuck on this it's the most frusterating thing ever. I am pretty new t networking but i can't seem to find anything on this. Nothing that works at least. Out of the millions of ways i have attempted to do this this is the final code i have written before I give up and ask the public for help:

    Code (CSharp):
    1.     [SyncVar] public int randMapSelection;
    2.     public GameObject[] levelsToSpawn;
    3.  
    4.     GameObject layout;
    5.  
    6.    
    7.     public override void OnNetworkSpawn()
    8.     {
    9.         randMapSelection = Random.Range(0, levelsToSpawn.Length);
    10.     }
    11.  
    12.     public void ActivateLevel()
    13.     {
    14.  
    15.         ActivateRoomClientRpc();
    16.  
    17.     }
    18.  
    19.  
    20.  
    21.     [ClientRpc]
    22.     void ActivateRoomClientRpc()
    23.     {
    24.         layout = Instantiate(levelsToSpawn[randMapSelection], transform);
    25. }
    26.        
    27.  
    ActivateLevel() is being called from a different script which has a trigger set up to it:

    Code (CSharp):
    1. public class SpawnLevel : NetworkBehaviour
    2. {
    3.     [SerializeField] GameObject generateLevel;
    4.  
    5.  
    6.     private void OnTriggerEnter(Collider other)
    7.     {
    8.            generateLevel.GetComponent<GlobalLevelSelector>().ActivateLevel();
    9.     }
    10. }
    11.  
    Like i said, it works perfectly fine to spawn a random gameobject but it doesn't sync what has been spawned.

    Can someone please help me with this. I have re-written these scripts so many times.
     
  2. ItsKody

    ItsKody

    Joined:
    Jun 11, 2019
    Posts:
    4
    Bump...
     
  3. arnaud_gout

    arnaud_gout

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    26
    Hi ItsKody,

    First of all, I think your post would get better answers in the Netcode for GameObjects forum!

    But, from what I understand, it seems like you are creating a new random number every time a new player is spawning, so even if the number is synchronized across every player, since you are regenerating a new number then immediately load a level at spawning time, every player will have a different level.
    We would suggest you to generate the random number somewhere else in your code, preferably when the server knows it's time to load a new level. Or only once if it has to be generated when the first player spawns.
    I hope this helps
    Good luck! And don't hesitate if you have more questions :)