Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why are my network ID's 0 on spawn?

Discussion in 'Multiplayer' started by joshuuua, Jan 27, 2023.

  1. joshuuua

    joshuuua

    Joined:
    May 1, 2015
    Posts:
    3
    Hi! I'm trying to keep an array of NetworkBehaviourReferences to pass to the client for a rendering task.

    My code does this:

    Code (CSharp):
    1.         HexCell cell = Instantiate(_cellPrefab);
    2.         cell.GetComponent<NetworkObject>().Spawn();
    3.         NetworkBehaviourReference cellRef = new NetworkBehaviourReference(cell); // all network behaviour ref is  0?
    4.         _cells[i] = cellRef;
    _cellPrefab has a class HexCell which inherits from NetworkBehaviour, as well as a NetworkObject component.
    When I look at the spawned HexCell in scene, it has a non-zero id as a network id.
    However, when I debug and inspect my cellRef, it has a network id of 0.

    Is there something I'm missing? There seems to be a lack of documentation on this online.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    Haven't used those before but I'm not surprised the id being zero. It is a struct, you are calling new on the struct, at which point the instance is created and all fields are initialized to default values. At the point you are assigning it to the cells array, the NetworkBehaviourReference has not been synchronized over the network. I also do not see NetworkVariable or RPC calls in your code snippet so maybe you misunderstood that struct as being automatically synchronizing? You have to write some synchronization code or put that cellRef in a NetworkVariable (or NetworkCollection).
     
  3. joshuuua

    joshuuua

    Joined:
    May 1, 2015
    Posts:
    3
    Right, I'll be synchronizing it elsewhere. This is on the host -- I was assuming that I could store the list of networkIds to send over a clientRPC later, and that by running new NetworkBehaviourReference() and passing in the object, I would be given some sort of id to track it with later. Does the binding of a network ID happen asynchronously?