Instantiate the game object for the local player only

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Instantiate the game object for the local player only

francvol
2021-03-23 20:45:23

Greetings to all, sorry for my writing, but I am Italian and I have translated the text automatically.
I have a problem that I have not been able to solve for 4 days, I am here to ask for a little help.
I'm trying to get checkpoints to work, my problem is that I add checkpoints via PhotonNetwork.Instantiate, so far everything works fine, but when I try to show or hide the checkpoints, the command has global effect.
I have made many attempts with photonView.IsMine, but have not been successful.
The goal is to show each player only their own checkpoints and not those of others.
I thank anyone who wants to help me.

using UnityEngine;  
using Photon.Pun;  
using System.Xml.Linq;  
using System.Linq;  
using System.Collections.Generic;

public class PUN2_RoomController : MonoBehaviourPunCallbacks  
{  
    public  List<GameObject> CheckPointsList;  
    //Player spawn point  
    public Transform[] spawnPoints;  
    //Player instance prefab, must be located in the Resources folder  
    public GameObject playerPrefab;  
    //CeckPoint instance prefab, must be located in the Resources folder  
    public GameObject CheckPointPrefab;  
      
    string NomeLivello;  
    Vector3 ___pos;  
    Quaternion ___rot;  
    void Awake()  
    {  
        NomeLivello = (string)PhotonNetwork.CurrentRoom.CustomProperties["Maps"];  
    }  
    // Use this for initialization  
    void Start()  
    {  
        //In case we started this demo with the wrong scene being active, simply load the menu scene  
        if (PhotonNetwork.CurrentRoom == null)  
        {  
            Debug.Log("Is not in the room, returning back to Lobby");  
            UnityEngine.SceneManagement.SceneManager.LoadScene("Carica_Online");  
            return;  
        }

        //We're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate  
        PhotonNetwork.Instantiate(playerPrefab.name, spawnPoints[Random.Range(0, spawnPoints.Length - 1)].position, spawnPoints[Random.Range(0, spawnPoints.Length - 1)].rotation, 0);  
          
        string levelIndex = NomeLivello;  
          
        TextAsset theList = (TextAsset)Resources.Load(levelIndex, typeof(TextAsset));  
        string txtContent = theList.text;  
        XDocument doc = XDocument.Parse(txtContent);  
        foreach (var dm in doc.Descendants("Oggetto"))  
        {  
            //---------------------  
            string nomeProps = dm.Element("PropsName").Value;  
            string Pos_X = dm.Element("PropsPosX").Value;  
            string Pos_Y = dm.Element("PropsPosY").Value;  
            string Pos_Z = dm.Element("PropsPosZ").Value;  
            string Rot_X = dm.Element("PropsRotX").Value;  
            string Rot_Y = dm.Element("PropsRotY").Value;  
            string Rot_Z = dm.Element("PropsRotZ").Value;  
            string Material = dm.Element("PropsMaterial").Value;  
            string IsCheck = dm.Element("PropsIsCheck").Value;  
            string IsDynamic = dm.Element("PropsIsDinamyc").Value;  
            string category = dm.Element("PropsCategory").Value;  
            string nameDefault = nomeProps.Substring(0, nomeProps.Length - 15);

            //Se è StartPoint imposta la posizione del player  
            if (nameDefault == "CheckPoint")  
            {  
                //xplapos = new Vector3(float.Parse(Pos_X), float.Parse(Pos_Y), float.Parse(Pos_Z));  
                //xplarot = new Vector3(float.Parse(Rot_X), float.Parse(Rot_Y), float.Parse(Rot_Z));  
                //CheckpointSystem_Online.pp = xplapos;  
                //CheckpointSystem_Online.pr = xplarot;  
                ___pos = new Vector3(float.Parse(Pos_X), float.Parse(Pos_Y), float.Parse(Pos_Z));  
                ___rot = new Quaternion(float.Parse(Rot_X), float.Parse(Rot_Y), float.Parse(Rot_Z), 0);  
                  
                    GameObject go = PhotonNetwork.Instantiate("CheckPoint\\" + "CheckPoint", ___pos, ___rot, photonPlayer.ID);  
                    go.name = nomeProps + PhotonNetwork.NickName;  
                  
            }

              
        }

          
            //TotCheckPoint = 0;  
            //ProgCheckPoint = 0;  
            //Colleziono tutti i checkpoint presenti e li inserico nella lista CheckPointsList

            CheckPointsList = GameObject.FindGameObjectsWithTag("CheckPoint").ToList();





            foreach (GameObject cp in CheckPointsList)  
            {  
                if (photonView.IsMine)  
                {  
                    cp.GetComponent<Renderer>().enabled = true;  
                }    
                Debug.Log(CheckPointsList.Count.ToString());  
            }  
            //ContaCheck.text = ProgCheckPoint.ToString() + "/" + TotCheckPoint.ToString();

            //Nascondo i checkpoint che non mi appartengono

         
    }

    void OnGUI()  
    {  
        if (PhotonNetwork.CurrentRoom == null)  
            return;

        //Leave this Room  
        //if (GUI.Button(new Rect(5, 5, 300, 100), "LEAVE RACE"))  
        //{  
        //    PhotonNetwork.LeaveRoom();  
        //}

        //Show the Room name  
        GUI.Label(new Rect(135, 5, 200, 25), PhotonNetwork.CurrentRoom.Name);

        //Show the list of the players connected to this Room  
        for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)  
        {  
            //Show if this player is a Master Client. There can only be one Master Client per Room so use this to define the authoritative logic etc.)  
            string isMasterClient = (PhotonNetwork.PlayerList[i].IsMasterClient ? ": MasterClient" : "");  
            GUI.Label(new Rect(5, 35 + 30 * i, 200, 25), PhotonNetwork.PlayerList[i].NickName + isMasterClient);  
        }  
    }

    public void EsciDallaGara()  
    {  
        PhotonNetwork.LeaveRoom();  
    }

    public override void OnLeftRoom()  
    {  
        //We have left the Room, return back to the GameLobby  
        UnityEngine.SceneManagement.SceneManager.LoadScene("MenùOnline");  
    }  
      
}  

Comments

JohnTube
2021-03-23 21:08:09

Hi @francvol,

Thank you for choosing Photon!

It's OK, most of us here are not native English speakers.

About your question: first of all why do you need to network instantiate if you need it only on the local client? use Unity's Instantiate then.

Otherwise, you may want to instantiate objects as hidden and unhide them locally then? or just do this via a script next to PhotonView and hide or show in Awake or Start?

francvol
2021-03-23 21:17:00

@JohnTube wrote: »

Hi @francvol,

Thank you for choosing Photon!

It's OK, most of us here are not native English speakers.

About your question: first of all why do you need to network instantiate if you need it only on the local client? use Unity's Instantiate then.

Otherwise, you may want to instantiate objects as hidden and unhide them locally then? or just do this via a script next to PhotonView and hide or show in Awake or Start?

The pleasure is mine, I thank you and the staff for this fantastic tool, sure of an excellent collaboration.

A to each checkpoint I assign a status, and this must necessarily be different between the players, for this reason I generate the same number of checks for each player

francvol
2021-03-24 21:18:17

I solved my problem.
I was practically trying to instantiate prefabs as inactive, but this is not allowed by Unity which automatically brings them back to active status.
While I was trying to enable only my items, the system enabled all checkpoints including those of other players.
It was enough to disable the other checkpoints to make my logic work correctly.
I hope this information will be useful.

Back to top