PhotonNetwork Instantiated gameobject getting destroyed.

I switch to Unity 2018.1.6 and i notice that after PhotonNetwork instantiates a gameobject, the gameobject get deleted after first frame. I tried replacing the PhotonNetwork.Instantiate() with Instantiate() and the gameobject remained. don't know if am doing anything wrong.

This is the code am using to spawn the gameobject. this is just an empty gameobject with photonView attached to it.


public class PlayerNetwork : MonoBehaviour
{
private PhotonView photonView;
private PhotonPlayer player;

// Use this for initialization
private void Awake()
{
photonView = GetComponent();
player = PhotonNetwork.player;
SceneManager.sceneLoaded += OnSceneFinishedLoading;
}

private void RoomConnSettings()
{
PhotonNetwork.sendRate = 60;
PhotonNetwork.sendRateOnSerialize = 30;
}

private void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
{
if (scene.name == "Example")
{
RoomConnSettings();
StartCoroutine(Wait());
}
}

private IEnumerator Wait()
{
Room room = PhotonNetwork.room;
if (! (bool) room.CustomProperties[Constants.ROUND_STARTED])
{
// make sure everyone is loaded?
yield return new WaitForSeconds(3);
if (PhotonNetwork.isMasterClient)
{
photonView.RPC("SpwanPlayer", PhotonTargets.AllBuffered, player.NickName);
room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable {{Constants.ROUND_STARTED, true}});
}
}
}


[PunRPC] private void SpwanPlayer(string name)
{
object[] instanceData = new object[4];
instanceData[0] = player.GetTeam();
instanceData[2] = LoadOut.instance.currentClass;

PlayerClass.Gender gender = User.instance.gender;
string path = string.Format("Characters\\{0}\\Default", gender.ToString());

GameObject GO = PhotonNetwork.Instantiate(path, new Vector3(0, 0, 0), Quaternion.identity, 0, instanceData);
GO.name = player.NickName;
}

}

Comments

  • Hi @chrys,

    does this happen on the MasterClient and / or on the other clients? I'm asking because this might be a problem with scene loading. Can you describe a bit more about the GameObject with the attached code snippet above? Is it placed in multiple scenes or just a single one? What is about the scenes at all? How many are there, when do they get loaded? The code snippet above looks okay as far as I can say.
  • It happens for both master and other clients.
    I made a short video Here the game object am spawning is the one by the right with name "Gameobject"
    when i get spawned, it gets named based on the script i posted above.

    Be default this gameobject has no scrip but i have to attach a script in the video for test.
  • And this code use to work before, it just stopped without me touching it.
  • Thanks for the video, it helped me getting a better overview about the situation.

    And this code use to work before, it just stopped without me touching it.


    So what did you do in the meantime? As far as I can see there shouldn't be any problem with it.

    Which PUN version do you currently use? Maybe updating to the latest version might help solving the problem.
  • I totally deleted photon from my project and installed again and still failing
  • Seems to be a problem with scene loading. As you have already added some Debug Logs to the Awake and the OnDestroy function of the networked object, I'm asking you to add another Debug.Log call to the Awake function of the PlayerNetwork class to see, in which order those logs appear in the console. If the OnDestroy log appears before the Awake log of the PlayerNetwork, this is a problem with scene loading. You can try to bypass this, by marking the networked object as DontDestroyOnLoad.
  • The Network log only shows once, when player connect, i dont know if this is right but i made another short video here
  • Can you please check the link, it is not working.
  • sorry HERE!