question

rootsnwonky avatar image
rootsnwonky asked

How to pass a value to the instantiated object? (PUN2)

The master-client object passed random "playerID". Other clients do not get "playerID" in their objects. Tried directly, tried via the function argument with PunRPC attribute (using PUN2).

All of those objects have PhotonViewer, their transforms sync and driven. Now just need to assign them "playerID" & tag.

Code:

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;
    }
}
unity3dphoton
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

Photon is the independent product separated from PlayFab. Such pure Photon technology questions, please navigate to Photon Engine Support Forum for more professional support.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.