PUN doesn't find in-scene prefabs when loading the scene second time around

Options
Sorry, the title is probably a bit confusing. I'm trying to do the following: I have a "Start Scene" in Unity, which is where the player starts and is off-line. when ready, they click a button to connect to photon and have it load the "Main Scene". Once the main scene is loaded, the player is instantiated by photon at the position and rotation of a prefab (ie, a spawn point). Once in the Main Scene, the player can click a button to disconnect from photon and return to the Start Scene.

The problem is, this works perfectly the first time around, but once the player goes back to the Start Scene and then tries to repeat, once the Main Scene is loaded for the second time, I get this error message:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.

The spawnpoint is just a prefab that I've placed into the scene. There is no photon view or anything on it. I'm only using it to read it's position, and I know it's there the second time because I can see it in the editor scene view, but photon thinks it isn't. I know I'm probably just using a wrong command or something, but I can't figure out what it is, and I have no idea why photon thinks it's been destroyed because it didn't instantiate it in the first place. Here is the only script in the Start Scene(heavily condensed for brevity):

public class StartSceneScript : Photon.PunBehaviour
{
  public bool offlineMode = false;
  public byte MaxPlayersPerRoom = 6;
	
  void Awake()
  {
    PhotonNetwork.logLevel = Loglevel;
    PhotonNetwork.autoJoinLobby = false;
    PhotonNetwork.automaticallySyncScene = false;
  }

  public void LoadLevel()
  {
    ConnectToPhoton();
  }
	
  void ConnectToPhoton()
  {
    if (!PhotonNetwork.connected)
    {
      PhotonNetwork.ConnectUsingSettings("1");
    }
  }

  public override void OnConnectedToMaster()
  {
    PhotonNetwork.JoinRandomRoom();
  }
	
  public virtual void OnPhotonRandomJoinFailed()
  {
     PhotonNetwork.CreateRoom(null, new RoomOptions() {MaxPlayers = MaxPlayersPerRoom}, null);
   }
  
  public override void OnJoinedRoom()
  {
     PhotonNetwork.LoadLevel("MainScene");
  }
}
And here is the only script used in the Main Scene:

public class MainSceneScript : Photon.MonoBehaviour
{
  public GameObject playerPrefab;
  public GameObject spawnSpot;
	
  void OnEnable()
  {
    SceneManager.sceneLoaded += OnSceneLoaded;
  }
	
  void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  {
    if(PhotonNetwork.connected)
    {
	SpawnPlayer();
    }
  }
	
  void SpawnPlayer()
  {
    GameObject player	= PhotonNetwork.Instantiate(playerPrefab.name, spawnSpot.transform.position, 
    spawnSpot.transform.rotation, 0);
  }
	
  public void LoadStartScene()
  {
    PhotonNetwork.Disconnect();
    SceneManager.LoadScene("StartScene", LoadSceneMode.Single);
  }
}
Note: I'm not even concerned about other players joining yet or anything else. I'm just trying to track down why this error is occuring. The only thing I can think of is that maybe "PhotonNetwork.Disconnect();" isn't the right thing to do, but I need the Start Scene to be off line again once the player goes back into it. I've tried everything I can think of. Any thoughts as to why this is happening or what the proper way to do this would be?

Comments

  • Tantei
    Options
    Add DontDestroyOnLoad for objects you want to preserve during scene load. For instantiated objects that you want around even after you leave the room try PhotonNetwork.InstantiateSceneObject