Adding a value to a NetworkList before spawning its networkObject results in a null ref exception
See original GitHub issueIf 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:
- Created a year ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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
And yeah, sure enough, we missed the same case for
NetworkList
. I’m willing to go inNetworkList.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: