Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

NetworkAnimator vs NetworkObjectPool

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

  1. KaneKennste


    Oct 1, 2019
    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


    Unity Technologies

    Feb 12, 2022
    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


    Oct 1, 2019
    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.