- Home /
Photon Unity : rename an instatiate GO and synchronize the name
hello everyone,
i'm quite new with Photon, i'm using PUN2 for a multiplayer game and i'm trying to change the game object name by using PunRPC.
the code seems to work for the local player, but not synchronizing along the network.
Please help me, it drives me crazy!
private void SpawnMyGroundPlayer(GameObject playerPosition)
{
myInstantPlayerGround= PhotonNetwork.Instantiate(playerGround.name, playerPosition.transform.position,playerGround.transform.rotation);
string MyPlayerId ="Player" + PhotonNetwork.LocalPlayer.ActorNumber;
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myPlayerName = MyPlayerId;
if (myActorNumber==1)
{
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myOpponentName = "Player" + (myActorNumber + 1);
}
else
{
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myOpponentName = "Player" + (myActorNumber- 1);
}
thePhotonView.RPC("RenameGO", RpcTarget.All);
}
[PunRPC]
private void RenameGO()
{
myInstantPlayerGround.name = "GROUND" + myActorNumber;
}
Answer by Clevereen · Apr 28, 2020 at 05:56 PM
Ok, i found my own answer!
The mistake that i've made, is using an RPC on a parent object, instead of using on the instantiated game object itself!
here what it should Looks
NetSpawn.cs
private void SpawnMyGroundPlayer(GameObject playerPosition)
{
myInstantPlayerGround= PhotonNetwork.Instantiate(playerGround.name, playerPosition.transform.position,playerGround.transform.rotation);
string MyPlayerId ="Player" + PhotonNetwork.LocalPlayer.ActorNumber;
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myPlayerName = MyPlayerId;
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myActorNumber = myActorNumber;
if (myActorNumber==1)
{
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myOpponentName = "Player" + (myActorNumber + 1);
}
else
{
myInstantPlayerGround.GetComponent<MyGroundPlayer>().myOpponentName = "Player" + (myActorNumber- 1);
}
}
then on the instantiate game object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Sirenix.OdinInspector;
using TMPro;
public class MyGroundPlayer : MonoBehaviourPun, IPunObservable
{
public int myActorNumber;
public PhotonView pView;
void Start(){
pView = this.GetComponent<PhotonView>();
if (photonView.IsMine)
{
pView.RPC("RPC_ChangeGroundName", RpcTarget.All, myActorNumber);
}
}
[PunRPC]
void RPC_ChangeGroundName(int myActorNumber_i)
{
this.gameObject.name = "Ground" + myActorNumber_i;
}
Your answer
Follow this Question
Related Questions
How to campare gameObject.name with string when gameObject have additional numbers or words? 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to obtain a gameObject from an List with the name? 1 Answer
How to make a progress bar for a network connection? 0 Answers