- Home /
Photon connections problems caused by a script?
This is the code that is creating the problem (more specifically it's the code inside the "foreach"):
void Start()
{
photonView = GetComponent<PhotonView>();
if (photonView.IsMine)
{
allGameObjects = FindObjectsOfType(typeof(GameObject));
foreach (GameObject objct in allGameObjects)
{
if (objct.tag != LayerMask.LayerToName(gameObject.layer) && objct.tag != "IgnoreDeletion")
{
Destroy(objct);
}
}
}
}
The problem is that after I build the game and run it twice the second player can't connect to the first player that created the room and gets a "no such room exists". That is the case, but I tried the same built game a few times and sometimes it works; I say this because I assume it's something that has to do with performance or not allowing enough time or something like that, hopefully it can help you know what the exact problem is.
What I want to do basically is delete everything in the scene for player1 that does not have to do with player1, and so on for every other player.
Player1 should only see player1 world AND the other players. Player2 should only see player2 world AND the other players.
I realize this is a bad way to make this but I have no clue how else it's supposed to be done.
Answer by GetLitGames · yesterday
I mean that code probably destroys almost all the objects in the game, if not all of them.. What are you trying to do? First off, you shouldn't destroy objects unless you created them in multiplayer. Second, try using GameObject.FindObjectsWithTag("tag") - your logic is wide open and it reads like it will destroy almost everything.. which I doubt is your intention.
The logic of the code does work; It destroys everything that does NOT have the same tag as the players layer (basically every object that does not belong in the players scene) and and leaves everything tagged "IgnoreDeletion".
I don't understand why this creates connection problems with photon though...?
What do you mean with "you shouldn't destroy objects unless you created them in multiplayer" tho?
Answer by viarsn · yesterday
My understanding of Photon is that each client loads the scene as if it was single player. Objects with photon views are synced (depends on which parts of that object you're syncing also, i.e., transforms). If player two joins the room, that doesn't mean that player one is going to see new items pop up and duplicated. Unless both clients are calling PhotonNetwork.Instantiate or RPC instantiations. Something is going confusingly wrong if when player two joins, his world is being duplicated on top of player ones world. Use PhotonNetwork.Destroy on photonviews if you want both clients to destroy the same object. If you do regular Destroy, only the player who called it will show it as destroyed.
Your answer
Follow this Question
Related Questions
Networking: Keep room alive without active players 0 Answers
Oculus Quest Photon Network bugs connecting players 2 Answers
How to connect to our own dedicated server using photon networking in unity?(Self-hosted) 0 Answers
Photon Pun 2 "using photon.pun" not working. 8 Answers
Is it possible to select one scene out of given option and make it public for others to join ? 0 Answers