Search Unity

Question Network players not loading into new scene

Discussion in 'Multiplayer' started by JoAsha, Mar 8, 2021.

  1. JoAsha

    JoAsha

    Joined:
    Dec 12, 2016
    Posts:
    12
    I'm using PUN 2 and at the moment players are able to connect from the initial scene to another one but then they do not load into any other scene after that.

    Code (CSharp):
    1. public void NextScene (int sceneIndex)
    2.     {
    3.         if (PhotonNetwork.IsMasterClient)
    4.         {
    5.             PhotonNetwork.AutomaticallySyncScene = true;
    6.             PhotonNetwork.LoadLevel(sceneIndex);
    7.         }
    8.     }
    No matter the order I do it, they only appear in the second scene (which instantiates them across the network using photon) but the above code does not work other than loading the scene. Am I missing something?
     
    ANLevant likes this.
  2. JoAsha

    JoAsha

    Joined:
    Dec 12, 2016
    Posts:
    12
    I am getting the error:

    PUN-instantiated 'Test(Clone)' got destroyed by engine. This is OK when loading levels. Otherwise use: PhotonNetwork.Destroy().
    UnityEngine.Debug:Log (object)

    How do I stop Unity from destroying PUN-instantiated objects?
     
    ANLevant and younessben2021 like this.
  3. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Hi! So the sync can be done before the connection stage of your game, meaning you set it before you actually connect. For example, in my projects I do the following in my start method. At this point we will connect and automatically sync scenes with any other people who are hosting a game that we join.
    Code (CSharp):
    1. //let's connect to our server if not already
    2.         if (!PhotonNetwork.IsConnected)
    3.         {
    4.             PhotonNetwork.AutomaticallySyncScene = true;
    5.             PhotonNetwork.ConnectUsingSettings();
    6.         }
    Your issue I believe is residing with your "AutomaticallySyncScene" location and you only wanting the master client to sync scenes and not everyone else based off that code. Hope that helps!
     
  4. JoAsha

    JoAsha

    Joined:
    Dec 12, 2016
    Posts:
    12
    Hi, thank you for your response! I have implemented your suggestion and unfortunately that has not resolved the issue. The scene is being loaded for all clients, however, Unity is destroying the players as they were instantiated in the previous scene. I am using PhotonNetwork.Instantiate but it is still deleting the objects.
     
  5. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    If you instantiate something in a scene and then change scenes you will lose the objects. The objects don't follow on their own naturally in Unity. If you want an object to remain even if a scene is changed then you would have to use the don't destroy on load method like so.
    Code (CSharp):
    1. DontDestroyOnLoad(this);
    This would allow objects to remain even when changing scenes. I've never used this though personally with my photon instantiated objects however. So I can't say how the photon ownership will be after the load, but I'd assume it would be fine.
     
  6. JoAsha

    JoAsha

    Joined:
    Dec 12, 2016
    Posts:
    12
    That was my understanding of how it works in Unity too, however, I was following this (
    ) tutorial as this type of scene loading is what I am after (start -> lobby -> game) and their players seem to persist despite the scene change. I have looked through all the other videos multiple times to see if there was a setting or a line of code I was missing but there isn't as far as I'm aware.

    I have been using DontDestroyOnLoad(this) whilst I work on other areas and it seems fine except I am having an issue with OnPhotonSerializeView where it never receives the canMove variable and if I click a player object in the hierarchy that has a PhotonTransformView script attached then the stream.ReceiveNext() receives the position which throws and error as it's not a bool. Not sure if this is related to the use of DontDestroyOnLoad(this) for the players but I can't find anything online as to why this is the case.

    Code (CSharp):
    1.  
    2.  
    3.     public void SetIfCanMove (int photonPlayerID, bool ifCanMove)
    4.     {
    5.         if (PhotonNetwork.LocalPlayer == PhotonNetwork.PlayerList[photonPlayerID])
    6.         {
    7.             Debug.LogError(this.name + " moves: " + ifCanMove);
    8.             canMove = ifCanMove;
    9.         }
    10.     }
    11.  
    12.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    13.     {
    14.         if (stream.IsWriting)
    15.         {
    16.             stream.SendNext(canMove);
    17.         }
    18.         else
    19.         {        
    20.             this.canMove = (bool)stream.ReceiveNext();
    21.         }
    22.     }
     
  7. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Ah, good ol' InfoGamer. I usually push new users to his videos as he's got a lot of content. Sometimes I feel like that can be a problem, but his videos are great for the most part.

    As for the lack of receiving the canMove did you drag your script into the observed components of your PhotonView? Also not sure why your else has the "this." in there. If canMove is accessed in the first part without it you wont need it for the second part either.

    I never used OnPhotonSerializeView as I've never needed to control specific package data being sent. But from what I gather you are trying to access specific data, in this case canMove which I'm assuming is a boolean you've established earlier. My guess would be that you are accessing it before it's set or created if you're getting errors from it.
     
  8. JoAsha

    JoAsha

    Joined:
    Dec 12, 2016
    Posts:
    12
    Yeah I have found some of his videos very helpful.

    OnPhotonSerializeView is working it's just receiving the wrong input (player position instead of the canMove boolean). I am using an RPC function instead which seems to be working fine I just dont understand what is causing the OnPhotonSerializeView to not be working properly.
     
  9. younessben2021

    younessben2021

    Joined:
    Jul 2, 2021
    Posts:
    2
    Same issue here , I wonder If you have found any solution ? :)