How to activate/make visible a child GameObject inside an Instantiated GameObject for all Players/Cl

Options

Hey guys! How are you? I searched a lot of content on the internet to try to solve my problem and I couldn't find it.

The question is: How to activate/make visible a child GameObject inside an Instantiated GameObject for all Players/Clients?

My GameObject activation code is in a child object, in the same object I first instantiated.

Thanks in advance. My code:

public class CharSelect : MonoBehaviourPunCallbacks, IPunObservable

{

  public GameObject[] characterPrefabs;

  public int selectedCharacter;

  public PhotonView pv;

  public void Start()

  {

    selectedCharacter = PlayerPrefs.GetInt("selectedCharacter");

    if (pv.IsMine)

    {

      pv.RPC("SelectChar", RpcTarget.Others);

      characterPrefabs[selectedCharacter].SetActive(true);

    }

  }

  [PunRPC]

  public void SelectChar()

  {

    characterPrefabs[selectedCharacter].SetActive(true);

  }

  public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)

  {

    if(stream.IsWriting)

    {

      stream.SendNext(characterPrefabs[selectedCharacter].activeSelf);

    }

    if (stream.IsReading)

    {

      characterPrefabs[selectedCharacter].SetActive((bool)stream.ReceiveNext());

    }

  }

Comments

  • Felipe
    Options

    SOLVE IT!

    I forgot to pass the variable "selectedCharacter" in SerializeView.

    With this code, i did it.

    public class CharSelect : MonoBehaviour, IPunObservable
    {
        public GameObject[] characterPrefabs;
        public int selectedCharacter;
        public PhotonView pv;
        public bool setActive;
        public void Start()
        {
            if (pv.IsMine && this.gameObject.activeSelf)
            {
                selectedCharacter = PlayerPrefs.GetInt("selectedCharacter");
                {
                    characterPrefabs[selectedCharacter].SetActive(true);
                    pv.RPC("SelectChar", RpcTarget.Others, selectedCharacter, characterPrefabs[selectedCharacter].transform.gameObject.GetComponent<PhotonView>().ViewID, setActive);
                }
            }
        }
    
    
        [PunRPC]
        public void SelectChar(int ViewID, bool setActive, int selectedCharacter)
        {
            pv = PhotonView.Find(ViewID);
            characterPrefabs[selectedCharacter].transform.gameObject.SetActive(setActive);
        }
        public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(selectedCharacter);
                stream.SendNext(characterPrefabs[selectedCharacter].gameObject.activeSelf);
            }
            else if (stream.IsReading)
            {
                selectedCharacter = ((int)stream.ReceiveNext());
                characterPrefabs[selectedCharacter].gameObject.SetActive((bool)stream.ReceiveNext());
            }
        }
    }