A
A
Alexander Pianov2020-01-08 16:11:07
C++ / C#
Alexander Pianov, 2020-01-08 16:11:07

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;
    }
*/
}

There is PhotonViewer on the objects, the objects are synchronized with each other, there are no problems with management.
All that is needed is the transfer of individual numbers and tags, which for some reason does not occur.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pyanov, 2020-01-12
@pr9niks

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 question

Ask a Question

731 491 924 answers to any question