After adding RPC methods to the script, PUN call backs stopped working.

Hello people!

After I add last 2 methods "public void SetReady(bool state){...}", "[PunRPC] private void RPC_ChangeReadyState(Player player, bool state){...}" and "OnEnable() { SetReady(false); }" PUN call backs e.g. OnPlayerEnteredRoom stopped working.
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerListings : MonoBehaviourPunCallbacks
{
    public GameObject list;
    public PlayerListing playerListing;

    private bool ready;
    private List<PlayerListing> listingsList = new List<PlayerListing>();

    private void Awake()
    {
        GetCurrentRoomPlayers();
    }
    private void OnEnable()
    {
        SetReady(false);
    }
    void GetCurrentRoomPlayers()
    {
        foreach (KeyValuePair<int, Player> playerInfo in PhotonNetwork.CurrentRoom.Players)
        {
            AddPlayerListing(playerInfo.Value);
        }
    }
    public void AddPlayerListing(Player player)
    {
        PlayerListing listing = Instantiate(playerListing, list.transform);
        if (listing != null)
        {
            listing.SetPlayerInfo(player);
            listingsList.Add(listing);
        }
    }
    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        AddPlayerListing(newPlayer);
    }
    public override void OnPlayerLeftRoom(Player otherPlayer)
    {
        int index = listingsList.FindIndex(x => x.Player == otherPlayer);
        if (index != -1)
        {
            Destroy(listingsList[index].gameObject);
            listingsList.RemoveAt(index);
        }
    }
    public void SetReady(bool state)
    {
        ready = state;
        Debug.Log(PhotonNetwork.LocalPlayer.NickName+ " is ready - " + ready);
        photonView.RPC("RPC_ChangeReadyState", RpcTarget.AllBuffered, PhotonNetwork.LocalPlayer, ready);
    }
    [PunRPC]
    private void RPC_ChangeReadyState(Player player, bool state)
    {
        int index = listingsList.FindIndex(x => x.Player == player);
        if (index != -1)
        {
            listingsList[index].ready = state;
            if (state)
                listingsList[index].readyText.text = "Ready";
            else
                listingsList[index].readyText.text = "Not Ready";
        }
    }
}

Can you tell me where am I wrong?
Thank you very much!

Best Answer

  • when this scriptOnEnable() it SetReady(false), and inside SetReady() it rpc.
    Could you make sure that this script instantiate after Photon InRoom?
    if this script run before Inroom, it might cause some sort of error.

Answers

  • when this scriptOnEnable() it SetReady(false), and inside SetReady() it rpc.
    Could you make sure that this script instantiate after Photon InRoom?
    if this script run before Inroom, it might cause some sort of error.
  • ThomasKao_XRSpace Thank you very much. I just changed OnEnabled to Start and everything works fine now.