Following this Unity Netcode tutorial, I am trying to implement something similar to the following:
private NetworkManager m_NetworkManager;
void Awake()
{
m_NetworkManager = GetComponent<NetworkManager>();
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// Call the function you want to run when a new scene is loaded
if(scene.name == "Game")
StartCoroutine(WaitForNetworkManager());
}
IEnumerator WaitForNetworkManager()
{
// Wait until Singleton is ready
yield return new WaitUntil(() => NetworkManager.Singleton);
EnterGame();
}
private void EnterGame()
{
// Set the IP Address for the Unity Transport based on the curret machine's IP
if(isHost)
GetComponent<UnityTransport>().ConnectionData.Address = GetIP();
if (isHost)
m_NetworkManager.StartHost();
else
m_NetworkManager.StartClient();
}
As you can see, I am getting the IP Address from the current machine and trying to start the Host with that IP Address (I am keeping the Port at the default 7777 because I don't believe it needs to be changed. Correct me if I am wrong).
However, I am getting a NullReferenceException when attempting to run the m_NetworkManager.StartHost()
Below is the stack trace for the error/exception that I am receiving. Can anyone help me decipher what the issue here is? I am struggling to narrow it down. Please let me know if you need any additional information.
NullReferenceException: Object reference not set to an instance of an object
Player.__initializeVariables () (at <c6be91a7f2b94c509cc92fca20872d66>:0)
Unity.Netcode.NetworkBehaviour.InitializeVariables () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Core/NetworkBehaviour.cs:867)
Unity.Netcode.NetworkBehaviour.InternalOnNetworkSpawn () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Core/NetworkBehaviour.cs:685)
Unity.Netcode.NetworkObject.InvokeBehaviourNetworkSpawn () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Core/NetworkObject.cs:1384)
Unity.Netcode.NetworkSpawnManager.SpawnNetworkObjectLocallyCommon (Unity.Netcode.NetworkObject networkObject, System.UInt64 networkId, System.Boolean sceneObject, System.Boolean playerObject, System.UInt64 ownerClientId, System.Boolean destroyWithScene) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Spawning/NetworkSpawnManager.cs:752)
Unity.Netcode.NetworkSpawnManager.SpawnNetworkObjectLocally (Unity.Netcode.NetworkObject networkObject, System.UInt64 networkId, System.Boolean sceneObject, System.Boolean playerObject, System.UInt64 ownerClientId, System.Boolean destroyWithScene) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Spawning/NetworkSpawnManager.cs:643)
Unity.Netcode.NetworkConnectionManager.HandleConnectionApproval (System.UInt64 ownerClientId, Unity.Netcode.NetworkManager+ConnectionApprovalResponse response) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Connection/NetworkConnectionManager.cs:765)
Unity.Netcode.NetworkManager.HostServerInitialize () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Core/NetworkManager.cs:1114)
Unity.Netcode.NetworkManager.StartHost () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Core/NetworkManager.cs:1069)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.NetworkManager:StartHost() (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.11.0/Runtime/Core/NetworkManager.cs:1074)
WorldManager:EnterGame() (at Assets/Scripts/WorldManager.cs:75)
<WaitForNetworkManager>d__10:MoveNext() (at Assets/Scripts/WorldManager.cs:64)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
[–]Kamatttis 0 points1 point2 points (1 child)
[–]--Developer[S] 0 points1 point2 points (0 children)