Locate Master Client GameObject using PhotonNetwork.IsMasterClient
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Locate Master Client GameObject using PhotonNetwork.IsMasterClient
JoãoVitor
2021-06-25 17:25:38
Hello everyone, i'm trying to find my masterClient using photon network, my software consists of instantiate a player at the scene and then find a script witch will save your gameobject if you are the masterClient then if you are not the master client you will only instantiate an sprite at masterClient canvas, but it's giving me a nullpointer when you are not the masterClient as masterClient activates twice.
those are both of my scripts
ps: sorry for my bad english.
script 1 : On Player prefab
[SerializeField] GameObject position;
private void Awake()
{
this.photonView.RPC("ChangeMyTag", RpcTarget.AllBuffered);
}
[PunRPC]
public void ChangeMyTag()
{
SceneMasterIdentify.Identify(gameObject, position, this.photonView.IsMine ,transform.gameObject); // find my master
if (PhotonNetwork.IsMasterClient)
{
if (transform.CompareTag("Player"))
{
if (this.photonView.IsMine)
{
transform.tag = "Master";
transform.GetComponent<InteracaoProfessor>().enabled = true;
transform.GetComponent<InteracaoObjeto>().enabled = false;
}
}
}
}
script 2 : On scene
static GameObject professor = null;//teacher
static GameObject posicaoProfessor = null;//teacher canvas position
public static void Identify(GameObject master,GameObject position, bool isMine,GameObject personagem)
{
if (PhotonNetwork.IsMasterClient)
{
GameObject g = PhotonNetwork.Instantiate("PhotonPrefabs/Teacher", position.transform.position, position.transform.rotation);
g.transform.SetParent(master.transform.GetChild(6));
professor = personagem;
posicaoProfessor = position;
}
else
{
Debug.Log(professor.name);//teacher gameobject name
Debug.Log(posicaoProfessor.name);// teacher canvas gameobject name
GameObject g = PhotonNetwork.Instantiate("PhotonPrefabs/Teacher", posicaoProfessor.transform.position, posicaoProfessor.transform.rotation);
g.transform.SetParent(professor.transform.GetChild(6));
}
}
Thanks in advance !
Comments
hannahmontanna
2021-06-29 10:34:57
i think your code might be a little over-engineered for what you are going for!
Try using (PhotonNetwork.IsMasterClient) to check and get a bool if you are a master client....and PhotonNetwork.MasterClient to get a Photon.Realtime.Player object representing the player who is the master client (should always be player 0 (1) unless custom logic.
So there really is no reason to do and RPC for all that....
In start, just call like
If (PhotonNetwork.IsMasterClient)
{
gameObject.Tag = "MasterClient"
}
else
{
gameObject.Tag = "Scrub";
}
Hey @hannahmontanna thank you for the reply ! , i still got the error message and the gameobject didin't change de tag, maybe is because i'm new in photon i still don't got it the right way to do that, i created a new script with less things like you said, here fallow my new script.
if (PhotonNetwork.IsMasterClient)
{
transform.tag = "Master";
masterNumber = PhotonNetwork.MasterClient.ActorNumber;
}
else
{
transform.tag = "Player";
Invoke("FindMaster", 4f);
}
}
public void FindMaster()
{
foreach (Player pla in PhotonNetwork.PlayerList)
{
if (pla.ActorNumber == masterNumber)
{
// debug with master name
Debug.Log("encontrou alguem com o mesmo actor number " + pla.NickName);
PhotonNetwork.MasterClient.TagObject = "Master";
// pla.TagObject = "Master";
}
}
master = GameObject.FindGameObjectWithTag("Master");
Debug.Log("nome do mestre " + master.name);
master.transform.GetChild(1).GetChild(1).GetChild(0).GetComponent<TMP_Text>().text = master.transform.GetChild(1).GetChild(1).GetChild(0).GetComponent<TMP_Text>().text + "*";
master.transform.GetChild(1).GetChild(2).GetChild(0).GetComponent<TMP_Text>().text = master.transform.GetChild(1).GetChild(2).GetChild(0).GetComponent<TMP_Text>().text + "*";
}
*repost
Hey hannahmontanna thank you for the reply ! , i still got the error message and the gameobject didin't change de tag, maybe is because i'm new in photon i still don't got it the right way to do that, i created a new script with less things like you said here fallow my new script
if (PhotonNetwork.IsMasterClient)
{
transform.tag = "Master";
}
else
{
transform.tag = "Player";
masterNumber = PhotonNetwork.MasterClient.ActorNumber;
Invoke("FindMaster", 4f);
}
}
public void FindMaster()
{
foreach (Player pla in PhotonNetwork.PlayerList)
{
if (pla.ActorNumber == masterNumber)
{
Debug.Log("encontrou alguem com o mesmo actor number " + pla.NickName);
// pla.TagObject = "Master";
PhotonNetwork.MasterClient.TagObject = "Master";
}
}
master = GameObject.FindGameObjectWithTag("Master");
Debug.Log("nome do mestre " + master.name);
master.transform.GetChild(1).GetChild(1).GetChild(0).GetComponent<TMP_Text>().text = master.transform.GetChild(1).GetChild(1).GetChild(0).GetComponent<TMP_Text>().text + "*";
master.transform.GetChild(1).GetChild(2).GetChild(0).GetComponent<TMP_Text>().text = master.transform.GetChild(1).GetChild(2).GetChild(0).GetComponent<TMP_Text>().text + "*";
}
hannahmontanna
2021-06-29 16:23:31
Can I ask why you are trying to find the master client like this? You can message them directly at any time with built in photon remote procedure call and raise event?
Hello again Hanna i happily come to inform i have managede to solve the problem ! :smile: , i was using like that because i need to find the master for each player and then do something to demostrate him as a master of the room, about the events i haven't masterded yet :( , but i'm still learning about events and photon 2.
here is my code, so basically for each player on their scene will found the master, if the master quit
probably i can just call this method again and its solved :)
about this way you mentioned you got any example ? or a link, i will love reading to try to improve
foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Player"))
{
players.Add(obj);
}
if (PhotonNetwork.IsMasterClient)
{
transform.tag = "Master";
}
else
{
transform.tag = "Player";
Invoke("FindMaster", 4f);
}
}
public void FindMaster()
{
foreach(GameObject p in players)
{
if (p.GetComponent<PhotonView>().Owner.NickName == PhotonNetwork.MasterClient.NickName)
{
p.tag = "Master";
master = p;
}
}
Thanks a lot !
hannahmontanna
2021-06-29 19:16:16
Hey, what I am saying, is you do no need to find the master, but if you did, you can ALWAYS find the master client via photo network.masterclient, even when the master client leaves, it is automatically switched via photon backend!!!!
hannahmontanna
2021-06-29 19:17:08
In any given room, the photon master client, regardless of what player is checking, will always be photo network.masterclient!!!
hannahmontanna
2021-06-29 19:18:45
And if you want to check a particular photon view, if it is master client, you can check
If (photonView.Owner == PhotonNetwork.MasterClient)
hello hanna , thanks for the tips :) i'm going to use them
Back to top