- Home /
Level Loading Over A Network: Level Loaded Event returning a null value?
Hey so I'm trying out my first ever multiplayer game so far I've found some really great resources and I'm using the awesome free Photon Unity Networking extension. Basically my problem is this, once the player connects to/ initializes the server I need them to be taken from the menu screen to the "game" scene and once that happens, I want to instantiate the players character.
I've got a NetworkManager script which works more or less exactly as outlined in this article here is my method for loading level once the player connects to the server
//Load Level so that the network does not send any messages or do anything funny while level is loading
private void LoadLevel(string level, int levelPrefix){
//Stop sending data in default channel
Network.SetSendingEnabled(0, false);
//Stop recieving data
Network.isMessageQueueRunning = false;
//Load level
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
StartCoroutine(Stall(2));
//Enable sending and recieving once again
Network.isMessageQueueRunning = true;
Network.SetSendingEnabled(0, true);
//Fire the level loaded event
levelLoaded();
}
IEnumerator Stall(float seconds){
yield return new WaitForSeconds(seconds);
}
The levelLoaded event has a handler on my GameManager script which then runs the SpawnPlayer function here
private void SpawnPlayer(){
Debug.Log ("Player Spawned");
Network.Instantiate(player,new Vector3(0,0,0), Quaternion.identity,0);
}
The level loads fine but the SpawnPlayer() is never called and I get this error message
NullReferenceException: Object reference not set to an instance of an object
NetworkManager.LoadLevel (System.String level, Int32 levelPrefix) (at Assets/Scripts/NetworkManager.cs:82)
NetworkManager.OnServerInitialized () (at Assets/Scripts/NetworkManager.cs:40)
UnityEngine.Network:InitializeServer(Int32, Int32, Boolean)
NetworkManager:StartServer() (at Assets/Scripts/NetworkManager.cs:34)
NetworkManager:OnClickStartServer() (at Assets/Scripts/NetworkManager.cs:45)
UnityEngine.EventSystems.EventSystem:Update()
I call DontDestroyOnLoad(this) on my NetworkManager in the Awake() function so its not that the function is being deleted. My guess is since events in c# require a reciever whats happening is that levelLoad is being called before the level is loaded and the GameManager object exists. Although that makes no sense to me since the event is fired well after the loadlevel call is being made.
I should note that my level load method is based off this article. I know they use the dreaded SendMessage instead of calling an event like I did (for what's it worth I tried their SendMessage method and it still didn't work). My JS isn't as up to snuff as it could be so it's possible I've mistranslated something here?
Your answer
Follow this Question
Related Questions
Stuck on ConnectingToNameServer 0 Answers
Photon Networking - Synchronization problem 1 Answer
Unity networking custom properties 0 Answers
Photon Networking Child Inconsistencies 0 Answers