Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Unity Multiplayer Player spawning with multiple instances of himself :(

Discussion in 'Multiplayer' started by A-AGames, Mar 20, 2020.

  1. A-AGames


    Nov 24, 2019
    Hello everyone,
    I am working with Pun in Unity and I have a problem with my spawning function for 2 months now, but I can't figure out how to solve it. I am searching for the solution for 2 months and I am very sad that I can't find the solution. Don't worry I wasn't just searching for the solution for 2 months, I've done some other things in my game, but now I would really like to solve this problem.
    My connecting behaviour works like that and is located in the "Network Manager" script:
    - My player is in the Menu and presses a button
    - The player connects and searches for a room if the button has been klicked
    - If there is no room, he creates a new room and chooses a random scene from a list and changes the scene to that
    - If there is a room, he simply joins the room

    The player who creates the room spawns once (with one instance of himself) and everything is fine. But if another player joins, he spawns with multiple prefabs of himself, so there are 4 instances of himself, but he can only control/ move one of these and the other ones aren't even visible for the other players, but for the local player who spawned.
    My spawning function is called in Start() in another script ("Level Manager") which is in the other scene.

    I don't know if I was explaining that well, but I'll also send a recording of this problem and my two scripts.

    My "Network Manager" script which is in the Menu and it's responsible for the connecting and joining (ignore the excluded code, I tried to make a game mode system, but I will do this later, after I solved my main problems):

    My "Level Manager" script which is in the other scene is responsible for spawning and other stuff which happens in the level ("Collider Occupied" just checks if a player is standing on the spawn point and it returns a boolean which is used in this script, so the player shouldn't spawn there and it also didn't work without this script):
    This is the important part for the spawning:
    And this is the whole script if anyone wants to see it:

    Here are the recordings:
    View of Player 1, who created the room (works perfectly):

    View of Player 2, who joined (doesn't work):

    Please help me! I am searching for 2 months for the solution

    Thank you very very much already in advance, I really appreciate the help
  2. Benjiz


    Sep 29, 2016
    Hello !
    I'm not an expert but it's possible that the PhotonNetwork.instantiate method needs to be called when all players are already in the same room (like in a lobby).

    Try the unity Instantiate method
  3. A-AGames


    Nov 24, 2019
    Hello, thank you very much for your reply. That wouldn't work at all, because the spawn should be synchronized on the network. The only thing I could do is to call an RPC method where I do the normal Unity instantiate method.
    I think that wouldn't fix the problem, but thank you anyways.

    I'd be very happy if someone else could give a good suggestion.
  4. tobiass


    Apr 7, 2009
    PUN contains a script OnJoinedInstantiate. It may be a help figuring out how / when to instantiate the player.
    Unity may be sneaky when you load scenes. It will run scripts when you do. It will destroy objects (locally) if you don't load additive.
    So maybe you're running the Instantiate code too often? Put a Debug.LogWarning() where you call PhotonNetwork.Instantiate() (in any location) and check the console (run the Editor as second user).

    PhotonNetwork.Instantiate() will create the networked object on any client in the room and anyone joining, by instantiating a prefab.