question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a value to a NetworkList before spawning its networkObject results in a null ref exception

See original GitHub issue

If we try to add something to a network list, BEFORE we spawn its network object, we get a null ref exception. In the code below from NetworkList.cs, m_NetworkBehaviour is null.

internal void MarkNetworkObjectDirty() { m_NetworkBehaviour.NetworkManager.MarkNetworkObjectDirty(m_NetworkBehaviour.NetworkObject); }

This was not the case before the latest release (1.0.1), we were able to manipulate the network list freely before actually spawning its network object.

System.NullReferenceException: Object reference not set to an instance of an object
  at Unity.Netcode.NetworkList`1[T].MarkNetworkObjectDirty () [0x00001] in E:\New folder\MyProject\Library\PackageCache\com.unity.netcode.gameobjects@1.0.1\Runtime\NetworkVariable\Collections\NetworkList.cs:68 
  at Unity.Netcode.NetworkList`1[T].HandleAddListEvent (Unity.Netcode.NetworkListEvent`1[T] listEvent) [0x0000f] in E:\New folder\MyProject\Library\PackageCache\com.unity.netcode.gameobjects@1.0.1\Runtime\NetworkVariable\Collections\NetworkList.cs:499 
  at Unity.Netcode.NetworkList`1[T].Add (T item) [0x0003d] in E:\New folder\MyProject\Library\PackageCache\com.unity.netcode.gameobjects@1.0.1\Runtime\NetworkVariable\Collections\NetworkList.cs:386 

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TheCaveOfWonderscommented, Aug 30, 2022

ok i’ll move forward with Don't modify NetworkVariables before they are spawned would be nice being able to manipulate them before spawn, as part of setting up objects/prefabs.

1reaction
jeffreyrainycommented, Aug 30, 2022

Thanks for the report!

@TheCaveOfWonders The current SDK recommendation is: “Don’t modify NetworkVariables before they are spawned”. There’s a bunch of edge cases we might not be handling correctly if you modify NetworkVariables before (or worse: during) spawning. We made a fix over 1.0.1 so that the exception above doesn’t occur for regular NetworkVariables:

NetworkVariableBase.cs

                if (m_NetworkBehaviour == null)
                {
                    Debug.LogWarning($"NetworkVariable is written to, but doesn't know its NetworkBehaviour yet. " +
                                     "Are you modifying a NetworkVariable before the NetworkObject is spawned?");
                    return;
                }
                m_NetworkBehaviour.NetworkManager.MarkNetworkObjectDirty(m_NetworkBehaviour.NetworkObject);

And yeah, sure enough, we missed the same case for NetworkList. I’m willing to go in NetworkList.MarkNetworkObjectDirty() and add a similar check with a similar log message. This will trade the exception for a warning. But, the usage that @alevastor suggests would result in a pretty high number of warnings.

The initial discussion with the team about having a silent NULL check there, instead of the warning, was that hiding errors was not the right approach. I can double-check, but I doubt we’ll change our mind.

So, take-aways:

  • we’ll turn the exception in a warning
  • please try to not write to NetworkVariables before spawning
  • maybe we would revisit this at some point, but don’t count on it. At the very least, we need to tackle support issues first.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Question - Error when trying to add a struct to a NetworkList
Hello, I am working on adding a scoreboard to my game, so I made a struct that holds player info, made it serializable,...
Read more >
NetworkVariable | Unity Multiplayer Networking
First parameter (Previous): The previous value before the value was ... a NetworkVariable when the associated NetworkObject isn't spawned.
Read more >
c# - What is a NullReferenceException, and how do I fix it?
I have some code and when it executes, it throws a NullReferenceException , saying: Object reference not set to an instance of an...
Read more >
Untitled
GitHub How netcode works, and what makes WebAug 26, 2022 · Adding a value to a NetworkList before spawning its networkObject results in...
Read more >
Unity-Technologies/com.unity.netcode.gameobjects
NetworkHide() of an object that was just NetworkShow() n produces a warning, as remote clients will not get a spawn/despawn pair.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found