Hi all, I have an issue I can’t quite figure out the right way to handle it.
I have my player fully initialized, carrying armor, weapons, etc…
Therefore, instead of recreating the player every time it moves from level to level I wanted to use “DontDestroyOnLoad”
Then when I switch levels, the first thing I do is look for the spawn point and update the players position to that point.
The level is loading, but if I don’t use a CoRoutine, when I perform a find I’m finding object from the first level, and none from the new level. If I use a coroutine and yield after the loadlevel, but before the find, then it never returns from the yield. I’m assuming because the object was deleted, but I placed a "Don’tDestroyOnLoad on one of the component classes of the player.
I’m assuming maybe only the component class is persisted between levels and not the player and all of its sub-components or I have another issue?
Is this the right idea, or should I be thinking about it differently?
I suspect the player is getting deleted too because I’m losing the camera. If I want everything to persist where do I put DontDestroyOnLoad?
public IEnumerator CoLoadLevel (GameObject player, string level, int levelPrefix, string spawnpoint)
{
Debug.Log("Jumping...");
// There is no reason to send any more data over the network on the default channel,
// because we are about to load the level, because all those objects will get deleted anyway
Network.SetSendingEnabled(0, false);
// We need to stop receiving because first the level must be loaded.
// Once the level is loaded, RPC's and other state update attached to objects in the level are allowed to fire
Network.isMessageQueueRunning = false;
// All network views loaded from a level will get a prefix into their NetworkViewID.
// This will prevent old updates from clients leaking into a newly created scene.
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
yield return 0; //new WaitForSeconds (1);
yield return 0; //new WaitForSeconds (1);
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data
Network.SetSendingEnabled(0, true);
// Notify our objects that the level and the network is ready
Debug.Log("Seding On Network Load Level");
// Find the SPAWN location in the new level and update the players position to this new location
//GameObject _spawn = (GameObject)GameObject.Find(spawnpoint);
foreach ( GameObject _portal in GameObject.FindGameObjectsWithTag("Portal"))
{
if ( _portal.name == spawnpoint )
{
// Set the player's current position equal to the position of the jump gate
player.transform.position = _portal.transform.position;
break;
}
}
Debug.Log("Send " + count.ToString() + " OnNetworkLoadLevel messages");
}