- Home /
Starting a client on standalone.exe not working but i works on editor play
This code snippet here causes the error below the code only on standalone exe. What could be the proleme? Thanks for help.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class MyNetworkManager : NetworkManager
{
public bool isAtStartup = true;
void Update()
{
if (isAtStartup)
{
if (Input.GetKeyDown(KeyCode.X))
{
SetPort();
StartServer();
StartClient();
// StartHost(); // same error for startclient() or starthost()
isAtStartup = false;
}
}
}
}
NullReferenceException: Object reference not set to an instance of an object at UnityEngine.Networking.NetworkTransform.SerializeMode3D (UnityEngine.Networking.NetworkWriter writer) [0x0005c] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkTransform.cs:251
at UnityEngine.Networking.NetworkTransform.OnSerialize (UnityEngine.Networking.NetworkWriter writer, Boolean initialState) [0x0005a] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkTransform.cs:195
at UnityEngine.Networking.NetworkIdentity.UNetSerializeAllVars (UnityEngine.Networking.NetworkWriter writer) [0x00010] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkIdentity.cs:421
at UnityEngine.Networking.NetworkServer.SendSpawnMessage (UnityEngine.Networking.NetworkIdentity uv, UnityEngine.Networking.NetworkConnection conn) [0x000d0] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkServer.cs:1403
at UnityEngine.Networking.NetworkServer.ShowForConnection (UnityEngine.Networking.NetworkIdentity uv, UnityEngine.Networking.NetworkConnection conn) [0x0000b] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkServer.cs:1202
at UnityEngine.Networking.NetworkConnection.AddToVisList (UnityEngine.Networking.NetworkIdentity uv) [0x0000d] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkConnection.cs:497
at UnityEngine.Networking.NetworkIdentity.AddObserver (UnityEngine.Networking.NetworkConnection conn) [0x000b9] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkIdentity.cs:715
at UnityEngine.Networking.NetworkServer.SetClientReadyInternal (UnityEngine.Networking.NetworkConnection conn) [0x001cb] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkServer.cs:1191
at UnityEngine.Networking.NetworkServer.SetClientReady (UnityEngine.Networking.NetworkConnection conn) [0x00000] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkServer.cs:1125
at UnityEngine.Networking.NetworkManager.OnServerReady (UnityEngine.Networking.NetworkConnection conn) [0x00024] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManager.cs:772
at UnityEngine.Networking.NetworkManager.OnServerReadyMessageInternal (UnityEngine.Networking.NetworkMessage netMsg) [0x00014] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkManager.cs:653
at UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) [0x0011f] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkConnection.cs:416
at UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) [0x00007] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkConnection.cs:372
at UnityEngine.Networking.NetworkConnection.TransportRecieve (System.Byte[] bytes, Int32 numBytes, Int32 channelId) [0x00000] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkConnection.cs:522
at UnityEngine.Networking.NetworkServer.InternalUpdate () [0x0020b] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkServer.cs:702
at UnityEngine.Networking.NetworkServer.Update () [0x0000c] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkServer.cs:564
at UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () [0x00000] in C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkIdentity.cs:909
(Filename: C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTransform.cs Line: 251)
Answer by Bunny83 · Dec 02, 2016 at 09:55 PM
You can't be client and server at the same time. This is a special case and is called "Host".
Use StartServer if this PC should be a dedicated server
Use StartHost if this PC should be a server as well as a client.
Use StartClient if this PC should by a client.
Never use two of those methods in the same application.
Answer by mercior · Nov 15, 2016 at 09:00 AM
I fixed the same error - I had a UNET build which worked as server in the editor but did not work when built as standalone.
The problem for me was that I had an object that disabled itself in OnStart() which contained objects that had NetworkIdentities.
I don't 100% understand why, but when building standalone those objects are cached by the server before Start() is called and then they are attempted to be transmitted to clients, but the server produces this error because he objects are no longer active.
It can help to use the line
LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Debug;
in a Start() somewhere (eg your NetworkManager) to get more debug info out of UNET. This is what led me to find out what was going on.
I am having the same issue, except I am not disabling any objects at all. All I am doing is starting the server with match$$anonymous$$aker.Create$$anonymous$$atch() after enabling matchmaker like I have before in other projects. The only difference is I am doing this all in the start() method.
I tried calling Create$$anonymous$$atch() in the Update() method on a mouse button trigger and it is causing the same issue. This is only happening in the builds and not the editor. The problem doesnt occur when I use the "stock" UNET Network HUD, but I want the game to connect/host when the game starts up.
EDIT: I think I fixed the issue. The problem seemed to be because the game was using the same scene for Online AND Offline, and that scene had an object with a NetworkIdentity. So it was probably an issue for Offline mode. I fixed it by creating an offline scene as a "Loading/Connecting" screen
Your answer
Follow this Question
Related Questions
Object not updating on host 1 Answer
How can a client connect more players to a network game? 1 Answer
NETWORKING: Start as Host if no Host; Otherwise Start as Client 0 Answers
Building Client and Server apps from single project 1 Answer
unity netcode object doesnt get declared on client when instantiated 0 Answers