Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
    293
    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.
     
  4. Spookalybits

    Spookalybits

    Joined:
    Mar 11, 2023
    Posts:
    2
    I'm having a similar issue per below error code when trying to respawn a previously despawned enemy. I'm using Unity v2022.2.9 with NGO v1.2.0 and for the NetworkObjectPool I've tried v2.5.0, 2.2.0, and 2.0.4. I've tried explicitly declaring the values for parameters at Spawn/Despawn, but the only ones I have right now are booleans so I don't think I'd be having the same issue as KaneKennste. Aside from that not really sure what to do/what I can do.

    NullReferenceException: Object reference not set to an instance of an object
    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)
     
  5. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    293
    @Spookalybits

    I am not sure what these versions pertain to: v2.5.0, 2.2.0, and 2.0.4?
    NGO has an experimental package out (v2.0.0-exp.2) and the most recent fully released package compatible with 2022 is v1.9.1.

    I would recommend updating to NGO v1.9.1 and see if that resolves your issue?
     
  6. Spookalybits

    Spookalybits

    Joined:
    Mar 11, 2023
    Posts:
    2
    Sorry, those version #'s were in reference to the github versions of the NetworkObjectPoolManager.

    I originally only saw NGO v1.2.0 in the Package Manager so figured that was the most recent version for my version of Unity. After copy/pasting the NGO reference link (com.unity.netcode.gameobjects) from the Multiplayer docs I was able to get v1.9.1, which does seem to have solved the issue.

    Thanks!
     
    NoelStephens_Unity likes this.