Search Unity

NetworkAnimator vs NetworkObjectPool

Discussion in 'Netcode for GameObjects' started by KaneKennste, Jun 9, 2023.

  1. KaneKennste

    KaneKennste

    Joined:
    Oct 1, 2019
    Posts:
    7
    Hi,
    i our game we're using the NetworkObjectPool to handle our monster spawning. But we've noticed an issue with monster animations. After a while monsters refuse to play animations properly. Simultaniously the following error is printed into the Host console:

    NullReferenceException: Object reference not set to an instance of an object
    Unity.Netcode.FastBufferWriter.Seek (System.Int32 where) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Serialization/FastBufferWriter.cs:147)
    Unity.Netcode.Components.NetworkAnimator.SendParametersUpdate (Unity.Netcode.ClientRpcParams clientRpcParams, System.Boolean sendDirect) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Components/NetworkAnimator.cs:827)
    Unity.Netcode.Components.NetworkAnimator.CheckForAnimatorChanges () (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Components/NetworkAnimator.cs:765)
    Unity.Netcode.Components.NetworkAnimatorStateChangeHandler.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Components/NetworkAnimator.cs:76)
    Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkUpdateLoop.cs:185)
    Unity.Netcode.NetworkUpdateLoop+NetworkPreUpdate+<>c.<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkUpdateLoop.cs:232)

    I took a look into the NetworkAnimator and it looks to me that m_ParameterWriter is only initialized when the class is created (however you may call it, not in a function, only as member). But it gets cleared when OnNetworkDespawn is called. Therefore, once a monster is despawned from the network, the NetworkAnimator is broken.

    Am i missing something? Is there something wrong with how i use the NetworkObjectPool?
    I get a monster from the pool by calling GetNetworkObject on the NetworkObjectPool. And i return it to the pool by calling ReturnNetworkObject on the NetworkObjectPool and then Despawn on the monster.
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    259
    KaneKennste,
    I would suggest updating to the most recent version of NGO. The issue you are describing has been fixed, since NGO v1.2.0, where m_ParameterWriter is now assigned a FastBufferWriter during the declaration of the property. So each NetworkAnimator instance will have that available until the NetworkAnimator is destroyed. There are other related NetworkAnimator fixes that should also help prevent these types of issues when using object pools.

    Let me know if updating to the most recent version resolves your issue?
     
  3. KaneKennste

    KaneKennste

    Joined:
    Oct 1, 2019
    Posts:
    7
    Hi NoelStephens_Unity,
    The mistake i made, was calling SetTrigger on NetworkAnimator before the gameobject was spawned. I've also updated to the newest version of NGO.
    Thank you.