Urgent Problem, I can't instantiate players.

Options
First of all, sry English is not my native language , I apologize in advance if I do a mistake.

I know when people read the word "Urgent" they think: we all have urgent problems dude... but I seriously have a problem with my demo that I need to find a quick solution. I've been working with Unity for around 3 years, and started the multiplayer adventure last year. I've started learning Photon in September of last year, and with some friends we areworking on a Thirs Person Multiplayer Shooter, thanks to this extremely helpful forum and the basic tutorial, I've managed to solve all the issues succesfully.

The Demo we are working is the final project of my VideoGame Design Degree, I must present the game this friday.

Everything was great, based on the basic tutorial we manage to build a functional demo, but our instructor asked us if we can do a Lobby Manager, where players can create a custom room with max kills, time... etc (pretty much before that request the player just started to play as soon as they were connected to photon).

The lobby manager is complete, the player can create a custom game with modes, maps and so forth (with CustomRoomProperties), then choose a character, and other players can join the match whenever they want to.

(TLDR) Here is the problem:

When the player enters the battle arena they have to click a button called "Ready", when its pressed we call PhotonNetwork.Instantiate to spawn the player, but it only works for the first player that pressed the button, the others players cant instantiate, unity shows this message when it try to spawn:

PUN-instantiated 'Player(Clone)' got destroyed by engine. This is OK when loading levels. Otherwise use: PhotonNetwork.Destroy(). UnityEngine.Debug:Log(Object)
PhotonView:OnDestroy() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:341)


Because of this only the first instantiated player can play, the others can see it but they cant spawn their own.

Searching in this forum there is a couple of users that have the same problem, they dont provide the solution, but they mention something related to how unity load a level. I can't figure out a solution yet.

We have a lot of scripts in the project and its kind of difficult to me explain all of them , but if you need extra information just tell me and I'll provide it asaic.

Thanks for your time.

Comments

  • [Deleted User]
    edited July 2018
    Options
    Hi @Tomsthreem,

    When the player enters the battle arena they have to click a button called "Ready", when its pressed we call PhotonNetwork.Instantiate to spawn the player, but it only works for the first player that pressed the button, the others players cant instantiate


    Is there any other thing done when clicking this button, for example scene loading as the console message mentions, or is it just the mentioned Instantiation call? Can you show the code snippet behind this button press?

    Which versions of Unity and PUN are you using?

    Can you maybe describe the basic flow of the application? For example: Connecting -> Joining Lobby -> Joining Room -> Instantiating. Also important to know is, when Scene Loading is done.
  • Tomsthreem
    edited July 2018
    Options
    Doubled post.
  • Tomsthreem
    edited July 2018
    Options
    I dont know what happened to a recent comment I've made answering Christian_Simon, so if it is doubled I'm really sry.

    Hey @Christian_Simon , thanks for your answer.

    The flow of the game is this:

    1. Start the game loads a menu Scene when you can play Offline, Online and change options.
    2. If you enter the Online menu you can create a room or join an existing one.
    3. As soon as you create or join a room, enters another menu when you can see the PlayerCount and player names in the room, and you can press a button named GO, with calls a PhotonNetwork.LoadLevel() with the name of the map choosed.
    4. This button is independent of each player, so if any of them (even the MasterClient) press the button the other players won't load the level until each of them press it.
    5. When you finished the load level, a menu will pop up with this text: " To start the match you need at least 2 players ready. ", I have a counter var with that increments each time the player finish load level. If this var is >= 2 the famous button "Ready" will pop up. Here is the script attached to the button:

    public void SpawnPlayer() { isSpawned = true; GameObject player = PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint.position, spawnPoint.rotation, 0) as GameObject; player.GetComponent<StateManager>().enabled = true; player.GetComponent<PlayerMovement>().enabled = true; player.GetComponent<PlayerManager>().enabled = true; cam.GetComponent<FreeCameraLook>().target = player.transform; }

    the playerPrefab has a photonView component attached to it.

    6. If any other user starts the game and joins the existing room, he can enter the game and joins the started match.

    Unity Version: 2018.1.5f1
    PUN Version: 1.91

    Things I've tried so far with results in the same error:

    1. Change PhotonNetwork.LoadLevel with Unity's SceneManager.LoadScene()
    2. Start the game with no Lobby Manager default lobby with joinrandomroon, just like the Basic Tutorial does, it worked before now it doesnt.
    3. Change PhotonNetwork.Instantiate with the unity's one.

    Thanks for your help.
  • Tomsthreem
    Options
    I forgot to mention, in the point number 5. when you enter the "Room Menu", there you can choose the character you want to pick.
  • Tomsthreem
    Options
    Pls help me, I tried everyhing and it still doesnt work. :s :
  • Tomsthreem
    Options
    I managed to solve the problem, :smile: But unfortunetly my solution is not the best I guess. I had to work an old version of the demo in Unity 2017.3.1f1, and gradually add all the stuff we created, step by step, verifing if the error occurs. I complete this process with the Demo fully working, with no error.

    Just for check, I opened the problematic version in Unity 2017.3.1f1, after the conversion the project doesnt have the error anymore. But as soon as I open that in 2018.1.5f1 the error appears.

    If you have this problem and don't know what to do I suggest you this:


    1. Try the project in a different version of Unity.
    2. Make sure you don't have singletons in your GameObject with a PhotonView attached, because Unity detects that and it destroy the incoming copies of the same GO.
    3. If you have a lobby manager in a different scene where the user actually plays, (an exclusive scene just for menus and stuff), or you have multiple battle scenes queued in your room, make sure the players spawn in the battle arena, not in the menu scene, cos maybe that cause some errors.
  • Hi @Tomsthreem,

    glad to hear that you have found a solution to the problem. However I'm wondering why this is not working with Unity 2018.

    By reading the application flow you described, I'm pretty sure, that this is a problem with Scene Loading:

    6. If any other user starts the game and joins the existing room, he can enter the game and joins the started match.


    This point makes me think of it: as soon as a client has joined the room, he will receive all the buffered messages, including the Instantiation calls made by other players. This client then instantiates the object immediately. When he now loads the other scene, this object gets destroyed and the warning you have mentioned in the original post is logged to the console. This one may be followed by other warnings, too.

    If you want to give Unity 2018 another chance, you can try to mark player objects as DontDestroyOnLoad to prevent them from being destroyed when loading another scene.