Answer the question
In order to leave comments, you need to log in
How to pass a value to an object created via Photon Unity Network?
Multiplayer script that creates player instances.
The master client object receives an ID, which is randomly generated.
Objects of other clients do not get their IDs - I tried it directly, through a function with the [PunRPC] attribute, as well as through its argument.
using UnityEngine;
using Photon.Pun;
using System.IO;
public class PhotonPlayer : MonoBehaviourPun
{
public PhotonView PV;
public int playerID;
public GameObject myUnit;
public void Start()
{
PV = GetComponent<PhotonView>();
playerID = Random.Range(0, GameSetUp.GS.spawnPoints.Length);
if (PV.IsMine)
{
myUnit = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "UnitAvatar"),
GameSetUp.GS.spawnPoints[playerID].position,
GameSetUp.GS.spawnPoints[playerID].rotation, 0);
PV.RPC("RPC_ID", RpcTarget.AllBuffered, playerID);
//myUnit.GetComponent<UnitSetup>().playerID = playerID;
//myUnit.tag = "Player" + playerID;
}
}
[PunRPC]
public void RPC_ID(int ID)
{
myUnit.GetComponent<UnitSetup>().playerID = ID;
myUnit.tag = "Player" + ID;
}
//версия без аргумента
/*
[PunRPC]
public void RPC_ID()
{
myUnit.GetComponent<UnitSetup>().playerID = playerID;
myUnit.tag = "Player" + playerID;
}
*/
}
Answer the question
In order to leave comments, you need to log in
Finally solved) Called the ID of the created PhotonViev object myUnit.GetComponent().ViewID and simply passed it to the function argument with the network attribute targeted at everyone (in my case with buffering). There I found the created object by its Photon View ID and created its instance: GameObject unit = PhotonView.Find(ID).gameObject;
Then you can assign any variables from the local client script to the instance. All clients will see the changes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question