Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Issue when spawning prefabs after updating netcode package

Discussion in 'Netcode for GameObjects' started by Svartskogen, Sep 9, 2023.

  1. Svartskogen

    Svartskogen

    Joined:
    Apr 17, 2017
    Posts:
    9
    So I'm having the following issue after updating Netcode for gameobjects from 1.2.0 to 1.5.2:

    Before, I would instantiate some network prefabs just after changing to a new scene the following way:

    Code (CSharp):
    1.  
    2. public override void OnNetworkSpawn()
    3. {
    4.         if (!IsServer)
    5.         {
    6.             return;
    7.         }
    8.  
    9.        
    10.         var networkExitPrefab = Instantiate(myNetworkPrefab);
    11.         networkExitPrefab.localPosition = somePosition;
    12.         networkExitPrefab.eulerAngles = direction;
    13.         networkExitPrefab.GetComponent<NetworkObject>().Spawn(true);
    14. }
    15.  
    Really simple code, which is on a network object that's already on the new scene being loaded, that is, this code should execute just after the scene load is completed (at least in the host?). And ensures that the code is only executed on the host.

    Before it was working just fine.

    But now, with updated Netcode for GO 1.5.2, I'm getting the following error on clients, plus obviously the prefabs don't seem to be insantiated;


    Code (CSharp):
    1. [Netcode] Destroy a spawned NetworkObject on a non-host client is not valid. Call Destroy or Despawn on the server/host instead.
    2. UnityEngine.Debug:LogError (object)
    3. Unity.Netcode.NetworkLog:LogError (string) (at Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Logging/NetworkLog.cs:34)
    4. Unity.Netcode.NetworkLog:LogServer (string,Unity.Netcode.NetworkLog/LogType) (at Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Logging/NetworkLog.cs:91)
    5. Unity.Netcode.NetworkLog:LogErrorServer (string) (at Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Logging/NetworkLog.cs:52)
    6. Unity.Netcode.NetworkObject:OnDestroy () (at Library/PackageCache/com.unity.netcode.gameobjects@1.5.2/Runtime/Core/NetworkObject.cs:556)
    I would guess it has to do with some race condition given that I'm trying to do this during scene change (using netcode scene manager), and it was just working by chance before and now it doesnt.

    Other prefabs being instantiated later during gameplay seem to work fine thought, my network prefabs list on the network manager looks fine.
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175