Unity, event runs twice, can't figure out why.

Hello guys, i'm making a Real-Time multiplayer combat game, but i'm stuck with a thing:
When i am in the room, i disconnect from the room and i reconnect back again, my event, OnLevelFinishedLoading runs twice, but i've added an condition for prevent spawn prefab on the lobby, but it spawn twice of them in the game scene. There's my script:
private void Awake () {
DontDestroyOnLoad (this.transform);

if (FindObjectsOfType (GetType ()).Length > 1) {
Destroy (gameObject);
}

PhotonNetwork.sendRate = 50;
PhotonNetwork.sendRateOnSerialize = 40;

SceneManager.sceneLoaded += OnSceneFinishedLoading;

}

private void Update () {
int rand = Random.Range (0, SpawnPoints.Length);

spawnPointSpawn = SpawnPoints[rand].transform.position;

if (Input.GetKeyDown (KeyCode.Escape)) {
// pHandler.despawnPlayer();
// PhotonNetwork.DestroyPlayerObjects (PhotonNetwork.player);
PhotonNetwork.LeaveRoom (becomeInactive: false);

}
}

public void createNewRoom () {
if (PhotonNetwork.connectedAndReady) {

PhotonNetwork.CreateRoom (photonB.createRoomInput.text, new RoomOptions () { MaxPlayers = 10 }, null);
} else {

Debug.Log ("Connessione non riuscita perche' lenta, riconnettendo..");
PhotonNetwork.Reconnect ();
}

}

public void joinOrCreateRoom () {
if (PhotonNetwork.connectedAndReady) {
RoomOptions roomOptions = new RoomOptions ();
roomOptions.MaxPlayers = 10;
PhotonNetwork.JoinOrCreateRoom (photonB.joinRoomInput.text, roomOptions, TypedLobby.Default);
} else {
Debug.Log ("Connessione non riuscita perche' lenta, riconnettendo..");
try { PhotonNetwork.Disconnect (); } catch { Debug.Log ("Non posso disconnettermi, perche' non sono collegato -->"); }
try { PhotonNetwork.Reconnect (); } catch { Debug.Log ("Non posso riconnettermi, perche' sono gia connesso: --> Riavvia il gioco"); } //(pConnectSettings.versionName);
}

}


public void cambialivello () {
SceneManager.LoadScene ("GameRemake");
}

public void OnJoinedRoom () {

cambialivello ();


Debug.Log ("Ti sei connesso alla stanza!");

}

public void OnLeftRoom () {

SceneManager.LoadScene ("Main");
Debug.Log ("Uscito dalla stanza");

}

public int listagiocatorinm;

public void OnSceneFinishedLoading (Scene scene, LoadSceneMode mode) {
if (scene.name == "GameRemake") {

spawnPlayer();



foreach (PhotonPlayer pl in PhotonNetwork.playerList) {
listagiocatorinm++;
}

Debug.Log (listagiocatorinm);
presence.partySize = listagiocatorinm;
presence.partyMax = 10;
presence.state = string.Format ("Playing");
presence.details = string.Format ("Fighting..");
isJoined = true;

InvokeRepeating ("updateRpcDiscordInterno", 0.0f, 15.0f);

}

}

void updateRpcDiscordInterno () {
DiscordRpc.UpdatePresence (ref presence);
}

private void spawnPlayer () {
//PhotonNetwork.Instantiate (Player.name, spawnPointSpawn, Quaternion.identity, 0);
var clone = PhotonNetwork.Instantiate (Player.name, spawnPointSpawn, Quaternion.identity, 0);
clone.name = "Player" + Random.Range(1, 400);
Debug.Log ("Spawnato il player");
}

A help would be grateful :smile:

Comments

  • Hi @RynoSvapos_21,

    please check if you have this script twice in the scene especially after returning to the Menu scene again. If it is part of that scene and marked as DontDestroyOnLoad, it will instantiate again, when loading the Menu scene for a second time, so that you will have two of them in the scene.
  • So for fix this i have to call for spawnPlayer in another script that is not don't destroy on load?
  • If the script (which is marked as DontDestroyOnLoad) is part of a scene which gets loaded again, the object, this script is attached to, gets instantiated again. So after returning to the Menu (I guess), you have that object twice in the scene and it will furthermore call spawnPlayer twice whenever a certain scene gets loaded. This probably stacks up when you return to the Menu scene more often.

    You have two options to avoid this. The first one is to use the Singleton pattern for the DontDestroyOnLoad marked object. You can check this page for an example.

    Another option is to use a third scene which is used as a loader scene. I have used this once in a project with another team and it worked fine. In this project we had important manager objects like the GameManager and such created in this special loader scene. After all of them had been instantiated, we loaded the Menu scene. Since the objects were marked as DontDestroyOnLoad, they were available until the application got terminated. Since we haven't loaded this special loader scene again, we only had one of the managers each.
  • Thanks so much @Christian_Simon, saved my project :blush:
Sign In or Register to comment.