- Home /
This is only activating the hat locally or an all clients or no clients.
I made this code to try to get my character customization form my silly little multiplayer game, but this does not activate the hat if the other client has their hat set to on. ignore the monical and parrot.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CosAct : MonoBehaviour {
public PhotonView photonView;
public int hatTrue = 0;
//public int monicalTrue = 0;
//public int parrotTrue = 0;
void Update(){
photonView = this.transform.GetComponent<PhotonView> ();
hatTrue = PlayerPrefs.GetInt ("Hat");
//monicalTrue = PlayerPrefs.GetInt("Monical");
//parrotTrue = PlayerPrefs.GetInt("Parrot");
if(hatTrue == 1){
Debug.Log ("set hat on");
ActHat();
}else{
return;
}
//if(monicalTrue == 0){
// ActMonical();
// }else{
// return;
// }
//if(parrotTrue == 0){
// ActParrot();
// }else{
// return;
// }
}
void localhatlayer(){
if(photonView.isMine){
this.transform.FindChild ("Hat").FindChild("botcy").gameObject.layer = 11;
this.transform.FindChild ("Hat").FindChild("topcy").gameObject.layer = 11;
}
}
[PunRPC]
void ActHat(){
transform.FindChild("Hat").gameObject.SetActive(true);
localhatlayer ();
}
void ActMonical(){
transform.FindChild("Monical").gameObject.SetActive(true);
}
void ActParrot(){
transform.FindChild("Parrot").gameObject.SetActive(true);
}
}
There are multiple problems with your provided code. void ActHat()
has a RPC attribute but is never called as RPC, which in this case might be okay. Secondly you are storing the current 'hat' in the PlayerPrefs. This might also be okay, but you are never changing the value (maybe it is set in some other class), so the hat stays the same. Also when changing the hat you have to disable the previous active one, since I guess they should not be stackable. Another thing is that you want to synchronize the player's look once the hat is changed, this is also not present in your provided code.
What you want to have is more or less the following flow: Update() -> photonView.is$$anonymous$$ine -> locally change the hat -> tell others somehow, that you changed your hat
The last step can be done by using RPCs, RaiseEvents or OnPhotonSerializeView, which might be the best way in this case.
Answer by SPOTNINJADUD7890 · Nov 04, 2016 at 06:52 PM
Thanks for the reply. I am changing the hat playerpref on another screen. I know how to change the hat localy. I dont really know what you mean by tell others somehow that you changed your hat because if i knew how to do that i wouldnt really be asking the question. But ty anyway if you could give a bit of sample code that would be great!
For example using a RPCs function call. This can be something like PhotonView.RPC("SomeFunction", PhotonTargets.Others, photonViewId, newHatId);
. On receiving side, check if photonViewId matches the current game object in order to apply changes to the correct object. Then change the hat by using the received ID of the hat or whatever identifier you are using for this scenario. You might want to check on the documentation again and also take a look at the Photon$$anonymous$$essageInfo received on RPC call and see if this might help you, selecting the correct game object.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
PUN - Choose random Hero for player 0 Answers
Unity Photon Transform movement applying to opposite prefab 1 Answer