PHOTON ENEMIES SCENE GLOBAL

Hi guys, when I connect a room and the enemies are spawned before per other player the enemies don't appear for me just for other player for spawned the enemies don't do part of the scene global but I use:
PhotonNetwork.automaticallySyncScene = true;
GameObject enemie= PhotonNetwork.InstantiateSceneObject ("zombieEasy", new Vector3(200, 2, 200), Quaternion.identity, 0, null) as GameObject;
It only appears to players who were present at the time of birth, but does not appear to those who later enter the room. Will I have to put some Update () mode script to update the enemies? Or JoinRoom () {Update enemies} ??

Comments

  • =/
  • Hi @TyHover,

    PhotonNetwork.InstantiateSceneObject(...) will be executed properly when called by the MasterClient, only. Hope you had that in mind.

    Besides that PhotonNetwork.InstantiateSceneObject(...) is a buffered call on the server, means that late-joining clients will also receive these calls unless the associated game objects get destroyed before. Means that this should work out of the box, you don't have to do additional implementation or function calls.

    Do you have this issue every time testing this scenario or does it just happen sometimes, irregularly. Which version of PUN and which version of Unity to you currently use?
  • Unity 5.5.1, PUN v1.81

    If the players are together the enemies appear to the two players but if a player enters after the enemies were born appears only to the player who was inside the room.
  • Christian_SimonChristian_Simon ✭✭✭
    edited June 19
    Please try updating to the latest PUN version, currently 1.84.1, and check if this issue still appears.

    If it still appears, please let us know and tells us how to reproduce this issue. I did a quick check on this and everything works as expected, no unwanted behaviour.
  • TyHoverTyHover
    edited June 19
    the error continue, I gonna record a video, for more details
  • here the video ->
  • Scenario: Second client joins after the MasterClient has started the game (the same case shown at the end of the youtube video) - can you please check if the SceneObject (tree) gets instantiated before the client loads the scene? It currently looks like the second player joins the room, instantiates the scene object and removes it immediately due to loading of another scene. You can check this by adding Debug.Log(...) calls to the Awake() and the OnDestroy() function of a script attached to the spawned object.
  • It is not possible to see Debug.Log, because one is in the game scene and another is in another menu scene.
  • =/
  • TyHoverTyHover
    edited July 3

    I did, but don't appear, just 1 client appear Debug.Log("tree spawner"). I put the script that is born the object in the lobby with the "do not load destroy" script and added Debug.Log () the spawn function:

    void Start(){
    
    gameObject.GetComponent<PhotonView>().RPC("Spawn", PhotonTargets.All , null);
    
    }
    
    [PunRPC]
    public void Spawn (){
    
    GameObject player = PhotonNetwork.InstantiateSceneObject ("treetest", new Vector3(200, 2, 200), Quaternion.identity, 0, null) as GameObject;
    Debug.Log("TREE SPAWN");
    
    
    }
    

    my connection script:

    using System.Collections.Generic;
    using UnityEngine;
    
    public class mt_Connection : Photon.MonoBehaviour
    {
        string PlayerName = "Player";
        string DefaultPlayerName = "Player";
    
    
        static mt_Lobby m_Lobby = null;
        static mt_Lobby Lobby
        {
            get
            {
                if (m_Lobby == null)
                    m_Lobby = FindObjectOfType<mt_Lobby>();
                return m_Lobby;
            }
        }
    
        List<PhotonPlayer> m_PlayerList = new List<PhotonPlayer>();
        public List<PhotonPlayer> PlayerList
        {
            get
            {
                m_PlayerList.Clear();
                m_PlayerList.TrimExcess();
                foreach (PhotonPlayer players in PhotonNetwork.playerList)
                {
                    m_PlayerList.Add(players);
                }
                return m_PlayerList;
            }
        }
    
    
        public void JoinServer(bool create, bool random)
        {
            if (create && !random) CreateRoom();
            else if (!create && !random) JoinRoom();
            else if (!create && random) JoinRandom();
        }
    
        void CreateRoom()
        {
            RoomInfo[] rooms = PhotonNetwork.GetRoomList();
            string roomName = Lobby.RoomName;
            for (int i = 0; i < rooms.Length; i++)
            {
                if (roomName.Equals(rooms[i].Name))
                {
                    roomName += "(" + PhotonNetwork.GetRoomList().Length + 1 + ")";
                }
            }
            Debug.Log("trying to create room: " + roomName);
    
            ExitGames.Client.Photon.Hashtable customPropertiesToSet = new ExitGames.Client.Photon.Hashtable();
            customPropertiesToSet.Add("MapName", Lobby.MapName);
            customPropertiesToSet.Add("GameMode", Lobby.GameMode);
            customPropertiesToSet.Add("MapImage", Lobby.SelectedMap);
            customPropertiesToSet.Add("GameLength", Lobby.GameLength);
            customPropertiesToSet.Add("Password", Lobby.Password);
            //customPropertiesToSet.Add("Team", Lobby.Team);
    
            string[] customProperties = new string[5];
            customProperties[0] = "MapName";
            customProperties[1] = "GameMode";
            customProperties[2] = "MapImage";
            customProperties[3] = "GameLength";
            customProperties[4] = "Password";
            PhotonNetwork.CreateRoom(roomName, new RoomOptions() { MaxPlayers = (byte)Lobby.MaxPlayers, IsVisible = true, IsOpen = true, CustomRoomProperties = customPropertiesToSet, CleanupCacheOnLeave = true, CustomRoomPropertiesForLobby = customProperties }, null);
    
        }
    
        bool JoinRoom()
        {
            Debug.Log("trying to join room: " + Lobby.RoomName);
            return PhotonNetwork.JoinRoom(Lobby.RoomName);
        }
    
        void JoinRandom()
        {
            Debug.Log("trying to join a random room");
            PhotonNetwork.JoinRandomRoom();
        }
    
        public void Reconnect()
        {
            if (PhotonNetwork.connectionStateDetailed != ClientState.Disconnected && PhotonNetwork.connectionStateDetailed != ClientState.PeerCreated)
            {
                PhotonNetwork.Disconnect();
            }
    
            PhotonNetwork.ConnectUsingSettings("0.1");
        }
    
        void OnJoinedLobby()
        {
            Lobby.currentWindow = mt_Lobby.CURRENTWINDOW.MAIN;
            Lobby.InvokeRepeating("ServerList", 1, Lobby.ServerListRefresh);
    
            if (PlayerName == DefaultPlayerName && PlayerPrefs.GetString("PlayerName") != "") PlayerName = PlayerPrefs.GetString("PlayerName");
            PhotonNetwork.playerName = PlayerName;
            Debug.Log("PlayerName set to " + PhotonNetwork.playerName);
        }
    
        void OnLeftLobby()
        {
    
        }
        void OnReceivedRoomListUpdate()
        {
            Debug.Log("rooms updated: " + PhotonNetwork.GetRoomList().Length);
            Lobby.MessageText(null, Color.red, 0);
        }
    
        void OnJoinedRoom()
        {
            Lobby.currentWindow = mt_Lobby.CURRENTWINDOW.NONE;
            Debug.Log("Joined Room");
    
            List<PhotonPlayer> playerList = new List<PhotonPlayer>();
            playerList.Clear();
            playerList = PlayerList;
            for (int p = 0; p < playerList.Count; p++)
            {
                Debug.Log("Players connected: " + playerList[p].NickName);
                if (playerList[p] != PhotonNetwork.player)
                {
                    if (playerList[p].NickName == PhotonNetwork.playerName)
                    {
                        PhotonNetwork.playerName = PhotonNetwork.playerName + "(" + 1 + ")";
                    }
                }
            }
    
            PhotonNetwork.LeaveLobby();
            //PhotonNetwork.LoadLevel(Lobby.mapList[Lobby.SelectedMap]);//comment out for UFPS if any errors occur, this will be handled in vp_MPCOnnection
        }
    
        void OnFailedToConnectToPhoton(DisconnectCause er)
        {
            Debug.LogWarning("mt_Connection: Failed To Connect To Cloud: " + er);
            Lobby.MessageText("Waiting for Internet Connection...", Color.red, 999, TextAnchor.UpperLeft);
            InvokeRepeating("Reconnect", 3.5f, 3.5f);
        }
    
        void OnConnectionFail(DisconnectCause er)
        {
            Debug.LogWarning("mt_Connection: Connection Failed: " + er);
            Lobby.MessageText("Waiting for Internet Connection...", Color.red, 999, TextAnchor.UpperLeft);
            InvokeRepeating("Reconnect", 3.5f, 3.5f);
        }
    
        void OnConnectedToMaster()
        {
            Debug.Log("Connected to Master");
            Lobby.MessageText("Connected to Master", Color.red, 3, TextAnchor.UpperLeft);
            CancelInvoke("Reconnect");
        }
    
        void OnPhotonRandomJoinFailed()
        {
            Debug.Log("mt_Connection: No random room available, so we create one.");
            CreateRoom();
        }
    }
    
  • =/
  • You don't need to send a RPC to execute Spawn function on each client because PhotonNetwork.InstantiateSceneObject can only be called by the MasterClient, non-MasterClients will log error messages to the console. Means that you can update the Start function with a PhotonNetwork.isMasterClient condition and call PhotonNetwork.InstantiateSceneObject if this condition is true.
    public void Start()
    {
        if (PhotonNetwork.isMasterClient)
        {
            PhotonNetwork.InstantiateSceneObject(...);
        }
    }
    Can you confirm that OnDestroy isn't called on the spawned object on the second client?

  • It still continues as before, if they have everything together, more if a client enters after can not see.
    video bellow, seguindo your script:

  • TobiasTobias admin
    Christian will have another look on Monday. Sorry for the delay. Please be patient.
  • I don't know if you already told us if OnDestroy function is called on the spawned object on the second client after joining the room, so please add the following to a script attached to the spawned object.
    public void OnDestroy()
    {
        Debug.LogError("Game Object destroyed", this);
    }
    It would be also helpful to see if it gets instantiated at all. Therefore please add the following to the same script.
    public void OnPhotonInstantiate(PhotonMessageInfo info)
    {
        Debug.LogError("Game Object instantiated", this);
    }
    This is called when the object gets instantiated through Photon. You can do the same thing in one of the initialization methods (Awake, Start, OnEnable).

    Please let me also know if the second client loads a level on his own (calling some kind of LoadLevel / LoadScene by himself) or just uses the PhotonNetwork.automaticallySyncScene. If automaticallySyncScene is enabled only the MasterClient should call PhotonNetwork.LoadLevel(...).
  • Does it work if you drag and drop your script to Observed Components in Photon View?
  • TyHoverTyHover
    edited July 13
    what script? and what observed components?
  • TyHoverTyHover
    edited July 14

    I don't know if you already told us if OnDestroy function is called on the spawned object on the second client after joining the room, so please add the following to a script attached to the spawned object.

    public void OnDestroy()
    {
        Debug.LogError("Game Object destroyed", this);
    }
    It would be also helpful to see if it gets instantiated at all. Therefore please add the following to the same script.
    public void OnPhotonInstantiate(PhotonMessageInfo info)
    {
        Debug.LogError("Game Object instantiated", this);
    }
    This is called when the object gets instantiated through Photon. You can do the same thing in one of the initialization methods (Awake, Start, OnEnable).

    Please let me also know if the second client loads a level on his own (calling some kind of LoadLevel / LoadScene by himself) or just uses the PhotonNetwork.automaticallySyncScene. If automaticallySyncScene is enabled only the MasterClient should call PhotonNetwork.LoadLevel(...).

    It is instantiating and then soon destroying the object, was what I could see in Debug.Log, but according to my script: what could be happening?

    //Universal Lobby for Photon, by M-Theory games - all rights reserved http://www.mtheory.net
    
    /*
     *td01 - Region select does not currently work with ConnectUsingSettings and thus will not support matchmaking
    */
    
    using System.Collections.Generic;
    using UnityEngine;
    
    public class mt_Connection : Photon.MonoBehaviour
    {
        string PlayerName = "Player";
        string DefaultPlayerName = "Player";
    
        /*TODO(td01)
          [HideInInspector]
           public CloudRegionCode region;
         * */
    
        static mt_Lobby m_Lobby = null;
        static mt_Lobby Lobby
        {
            get
            {
                if (m_Lobby == null)
                    m_Lobby = FindObjectOfType();
                return m_Lobby;
            }
        }
    
        List m_PlayerList = new List();
        public List PlayerList
        {
            get
            {
                m_PlayerList.Clear();
                m_PlayerList.TrimExcess();
                foreach (PhotonPlayer players in PhotonNetwork.playerList)
                {
                    m_PlayerList.Add(players);
                }
                return m_PlayerList;
            }
        }
    
    	void Awake(){
    
    		PhotonNetwork.automaticallySyncScene = true;
    
    	}
    
        public void JoinServer(bool create, bool random)
        {
            if (create && !random) CreateRoom();
            else if (!create && !random) JoinRoom();
            else if (!create && random) JoinRandom();
    
        }
    
        void CreateRoom()
        {
    		
            RoomInfo[] rooms = PhotonNetwork.GetRoomList();
            string roomName = Lobby.RoomName;
            for (int i = 0; i < rooms.Length; i++)
            {
                if (roomName.Equals(rooms[i].Name))
                {
                    roomName += "(" + PhotonNetwork.GetRoomList().Length + 1 + ")";
                }
            }
            Debug.Log("trying to create room: " + roomName);
    
            ExitGames.Client.Photon.Hashtable customPropertiesToSet = new ExitGames.Client.Photon.Hashtable();
            customPropertiesToSet.Add("MapName", Lobby.MapName);
            customPropertiesToSet.Add("GameMode", Lobby.GameMode);
            customPropertiesToSet.Add("MapImage", Lobby.SelectedMap);
            customPropertiesToSet.Add("GameLength", Lobby.GameLength);
            customPropertiesToSet.Add("Password", Lobby.Password);
            //customPropertiesToSet.Add("Team", Lobby.Team);
    
            string[] customProperties = new string[5];
            customProperties[0] = "MapName";
            customProperties[1] = "GameMode";
            customProperties[2] = "MapImage";
            customProperties[3] = "GameLength";
            customProperties[4] = "Password";
            PhotonNetwork.CreateRoom(roomName, new RoomOptions() { MaxPlayers = (byte)Lobby.MaxPlayers, IsVisible = true, IsOpen = true, CustomRoomProperties = customPropertiesToSet, CleanupCacheOnLeave = true, CustomRoomPropertiesForLobby = customProperties }, null);
    
        }
    
        bool JoinRoom()
        {
            Debug.Log("trying to join room: " + Lobby.RoomName);
            return PhotonNetwork.JoinRoom(Lobby.RoomName);
    
    	
        }
    
        void JoinRandom()
        {
            Debug.Log("trying to join a random room");
            PhotonNetwork.JoinRandomRoom();
    
        }
    
        public void Reconnect()
        {
            if (PhotonNetwork.connectionStateDetailed != ClientState.Disconnected && PhotonNetwork.connectionStateDetailed != ClientState.PeerCreated)
            {
                PhotonNetwork.Disconnect();
            }
    
            PhotonNetwork.ConnectUsingSettings("0.1");
        }
    
        void OnJoinedLobby()
        {
            Lobby.currentWindow = mt_Lobby.CURRENTWINDOW.MAIN;
            Lobby.InvokeRepeating("ServerList", 1, Lobby.ServerListRefresh);
    
            if (PlayerName == DefaultPlayerName && PlayerPrefs.GetString("PlayerName") != "") PlayerName = PlayerPrefs.GetString("PlayerName");
            PhotonNetwork.playerName = PlayerName;
            Debug.Log("PlayerName set to " + PhotonNetwork.playerName);
        }
    
        void OnLeftLobby()
        {
    
        }
        void OnReceivedRoomListUpdate()
        {
            Debug.Log("rooms updated: " + PhotonNetwork.GetRoomList().Length);
            Lobby.MessageText(null, Color.red, 0);
        }
    
        void OnJoinedRoom()
        {
            Lobby.currentWindow = mt_Lobby.CURRENTWINDOW.NONE;
            Debug.Log("Joined Room");
    
            List playerList = new List();
            playerList.Clear();
            playerList = PlayerList;
            for (int p = 0; p < playerList.Count; p++)
            {
                Debug.Log("Players connected: " + playerList[p].NickName);
                if (playerList[p] != PhotonNetwork.player)
                {
                    if (playerList[p].NickName == PhotonNetwork.playerName)
                    {
                        PhotonNetwork.playerName = PhotonNetwork.playerName + "(" + 1 + ")";
                    }
                }
            }
    
            PhotonNetwork.LeaveLobby();
    		PhotonNetwork.LoadLevel (Lobby.RoomName);
            //PhotonNetwork.LoadLevel(Lobby.mapList[Lobby.SelectedMap]);//comment out for UFPS if any errors occur, this will be handled in vp_MPCOnnection
        }
    
        void OnFailedToConnectToPhoton(DisconnectCause er)
        {
            Debug.LogWarning("mt_Connection: Failed To Connect To Cloud: " + er);
            Lobby.MessageText("Waiting for Internet Connection...", Color.red, 999, TextAnchor.UpperLeft);
            InvokeRepeating("Reconnect", 3.5f, 3.5f);
        }
    
        void OnConnectionFail(DisconnectCause er)
        {
            Debug.LogWarning("mt_Connection: Connection Failed: " + er);
            Lobby.MessageText("Waiting for Internet Connection...", Color.red, 999, TextAnchor.UpperLeft);
            InvokeRepeating("Reconnect", 3.5f, 3.5f);
        }
    
        void OnConnectedToMaster()
        {
            Debug.Log("Connected to Master");
            Lobby.MessageText("Connected to Master", Color.red, 3, TextAnchor.UpperLeft);
            CancelInvoke("Reconnect");
        }
    
        void OnPhotonRandomJoinFailed()
        {
            Debug.Log("mt_Connection: No random room available, so we create one.");
            CreateRoom();
        }
    }
  • 
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Spawn : MonoBehaviour {
    
    	// Use this for initialization
    	void Start () {
    		
    	}
    	
    	// Update is called once per frame
    	[PunRPC]
    	void Update () {
    
    		if (Input.GetKeyDown ("e")) {
    
    			if (PhotonNetwork.isMasterClient) {
    
    
    				GameObject player = PhotonNetwork.InstantiateSceneObject ("treetest", new Vector3 (200, 2, 200), Quaternion.identity, 0, null) as GameObject;
    
    			}
    
    
    		}
    
    	}
    
    	public void OnDestroy()
    	{
    		Debug.LogError("Game Object destroyed", this);
    	}
    
    	public void OnPhotonInstantiate(PhotonMessageInfo info)
    	{
    		Debug.LogError("Game Object instantiated", this);
    	}
    }
    

    And also does not have any other script destroying this object because this script is the only one, can be some script error in my connection script above?

  • help
  • If the game object gets instantiated and gets destroyed immediately afterwards, then it might be a problem with scene loading. Please add OnLevelWasLoaded callback (also it's obsolete) to either a game object that is marked as DontDestroyOnLoad or on a game object which is placed in the loaded scene (I guess it's the 'game' scene). Also add some Debug Logging to this callback and see when it gets called. Maybe it gives us some hints if the object gets destroyed by scene loading or we have some other problem.
  • TyHoverTyHover
    edited July 22

    If the game object gets instantiated and gets destroyed immediately afterwards, then it might be a problem with scene loading. Please add OnLevelWasLoaded callback (also it's obsolete) to either a game object that is marked as DontDestroyOnLoad or on a game object which is placed in the loaded scene (I guess it's the 'game' scene). Also add some Debug Logging to this callback and see when it gets called. Maybe it gives us some hints if the object gets destroyed by scene loading or we have some other problem.

    Thanks @Christian_Simon , with this I could see that I have 2 more script trying to open a new scene so I closed script part with //, and it worked normally thanks <3

    if you can help with this: http://forum.photonengine.com/discussion/comment/35546#Comment_35546
Sign In or Register to comment.