I am trying to tag two PUN instantiated game objects with "Player1" and "Player2" tags by looking at their PhotonView ViewIDs through an RPC call. I am able to successfully tag the player 1 game object with the player 1 tag, however, no matter what I try, I am unable to set the player2 tag to the player2 object. The code is networked and running on two Oculus Quest headsets. I can start the application on one Quest and it will assign the Player1 tag properly. However, when I start the application on the second Quest, it spawns a player gameobject, but does not tag the object with the Player2 tag even though the player 2 object's PhotonView matches the "2001" value. Below is the code that I am using to spawn in an XROrigin and a networked representation for each player.
using System;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.XR.Interaction.Toolkit;
public class NetworkPlayerSpawner : MonoBehaviourPunCallbacks
{
public GameObject XROriginPrefab;
[HideInInspector]
public GameObject spawnedPlayerPrefab;
private PhotonView pv;
private void Start()
{
pv = GetComponent<PhotonView>();
}
private void Update()
{
// Debug.Log(PhotonNetwork.CurrentRoom.PlayerCount);
}
public override void OnJoinedRoom()
{
base.OnJoinedRoom();
var playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
Debug.Log("The player count is: " + playerCount);
var teleportAreas = GameObject.FindGameObjectsWithTag("Floor");
//playerCount = 2;
if (playerCount == 1)
{
XROriginPrefab = Instantiate(XROriginPrefab, new Vector3(0, 2.36199999f, 3.78999996f),
new Quaternion(0, 0, 0, 1));
spawnedPlayerPrefab = PhotonNetwork.Instantiate("Network Player", transform.position, transform.rotation);
//spawnedPlayerPrefab.tag = "Player1";
foreach (GameObject go in teleportAreas)
{
go.AddComponent<TeleportationArea>();
}
}
else
{
XROriginPrefab = Instantiate(XROriginPrefab, new Vector3(-10.3859997f,1.60699999f,10.6400003f),
new Quaternion(0,0,0,1));
spawnedPlayerPrefab = PhotonNetwork.Instantiate("Network Player", transform.position, transform.rotation);
//spawnedPlayerPrefab.tag = "Player2";
//If teleport breaks again, I uncommented this line, so it should be commented out again. Should allow for teleport in User 2's room.
foreach (GameObject go in teleportAreas)
{
go.AddComponent<TeleportationArea>();
}
}
rpcCallTagAssign();
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
base.OnPlayerEnteredRoom(newPlayer);
Debug.Log("Remote Player Joined!");
rpcCallTagAssign();
}
public override void OnLeftRoom()
{
base.OnLeftRoom();
PhotonNetwork.Destroy(spawnedPlayerPrefab);
}
[PunRPC]
private void tagAssign()
{
if (spawnedPlayerPrefab.GetComponent<PhotonView>().ViewID==1001)
{
spawnedPlayerPrefab.tag = "Player1";
}
if (spawnedPlayerPrefab.GetComponent<PhotonView>().ViewID==2001)
{
spawnedPlayerPrefab.tag = "Player2";
}
}
private void rpcCallTagAssign()
{
pv.RPC("tagAssign", RpcTarget.AllViaServer);
}
}
I am new to networking with Photon, so any help with resolving this issue would be greatly appreciated. Thank you!
The code needs to run on each player (including the copies). The current code can only change the object you have a reference for (spawnedPlayerPrefab). The easiest way is to add an RPC function on the player. That RPC would get called for each instance of that player across the connected clients.
Script On Player.
[PunRPC]
private void AssignTag()
{
if (photonView.ViewID == 1001)
{
gameObject.tag = "Player1";
}
else if (photonView.ViewID == 2001)
{
gameObject.tag = "Player2";
}
}
In NetworkPlayerSpawner
spawnedPlayerPrefab = PhotonNetwork.Instantiate(...);
spawnedPlayerPrefab.GetComponent<PhotonView>().RPC("AssignTag", RpcTarget.AllBufferedViaServer);
The RPC is buffered so future clients entering after you will set your player copy to the correct tag as well (or vice versa).