Unity Unet NullReferenceException: Object reference not set to an instance of an object
I've been making a very simple multiplayer fps game using unet, everything has been going fine but today i'm getting an error from the networkmanager component by unity when i click lan host it doesn't spawn my player but other clients can still join me and there players are spawned this is the error I get in the console
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkIdentity.OnStartAuthority () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:377)
UnityEngine.Networking.NetworkIdentity.ForceAuthority (Boolean authority) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:112)
UnityEngine.Networking.NetworkServer.SpawnObjects () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1744)
UnityEngine.Networking.NetworkManager.StartServer (UnityEngine.Networking.Match.MatchInfo info, UnityEngine.Networking.ConnectionConfig config, Int32 maxConnections) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:347)
UnityEngine.Networking.NetworkManager.StartServer (UnityEngine.Networking.Match.MatchInfo info) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:269)
UnityEngine.Networking.NetworkManager.StartServer () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:264)
UnityEngine.Networking.NetworkManager.StartHost () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:508)
UnityEngine.Networking.NetworkManagerHUD.OnGUI () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManagerHUD.cs:75)
please help it's brought a halt to my project.
Answer by dariuszpietrala · Mar 10, 2016 at 10:05 PM
Hi, you probably have multiple NetworkIdentity scripts on one of the objects that is spawned on the server. I had the same problem today. Was doing some changes to the prefabs and the editor duplicated the script.
Answer by Vinny-the-true · Jan 31, 2017 at 10:38 AM
I'm having the exact same situation, but the prefab I'm trying to spawn doesn't contain more than one NetworkIdentity component... In fact, the exact same spawning works very well from another scene...
Answer by vanmani · Apr 08, 2016 at 04:48 AM
As darius says, it's caused by having multiple NetworkIdentities on a single object. In my case this happened during an automated merge (thanks git).