Photon RPC not sending on server.

I have a script, like so:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;


public class selectPiece : MonoBehaviour
{
    public Image render;
    public PhotonView photonView;

    public bool hasBeenSelected;
    public Color pieceSelected;


    void Update()
    {
        if (hasBeenSelected)
        {
            render.color = pieceSelected;
        }
    }


    public void selectedPiece()
    {
        if (!hasBeenSelected)
        {
            photonView.RPC("selection", RpcTarget.All);
            Debug.Log("Selected");
        }
    }


    [PunRPC]
    void selection()
    {
        hasBeenSelected = true;
    }
}

It works on one end, but never on the other, why? I've been trying to figure this dumb thing out for days.

I've implemented RPC in other scripts and they DO work, just never this single one, for some really odd reason. (photonView has been assigned.)

Any help would be appreciated, thank you.

Answers

  • Bump, as this has not been solved yet.

  • Hi @AxiiPop If you don't mind I will make a few comments which may help now but should help in the long run. For consistency sake don't start a C# class with a lowercase letter. There is no upside, it is confusing and nobody does this is the downside.

    Your Update() method is going to continually reassign the pieceSelected Color though nothing will have changed after the first time.

    Like the class name avoid starting C# method names with lowercase.

    Now... what makes you believe that the selection() method (you'll rename this right?) isn't being called? To verify it you should add Log statement in there.

    Finally what makes it all work? How does the pieceSelected color get set and what calls the selectedPiece() method?

    I'll suggest when you encounter these "this should work" issues that you step back and simplify so you can see something work. So add logging that shows you the method called, the value of variables and such. You may see things aren't flowing the way you suspected. Granted changing a color isn't "tricky" but it is possible for that to be too subtle to notice and Debug.Log statements will be obvious.

  • Finally what makes it all work? How does the pieceSelected color get set and what calls the selectedPiece() method?

    I have a button connected to the gameObject. It of course, triggers that method. It works locally but the RPC isn't being sent to the other clients.

    Granted changing a color isn't "tricky" but it is possible for that to be too subtle to notice and Debug.Log statements will be obvious.

    I have added a few log statements, nothing has changed about the problem.


    UPDATED CODE:

    using UnityEngine;
    using UnityEngine.UI;
    using Photon.Pun;
    
    
    public class selectPiece : MonoBehaviour
    {
        public Image render;
    
    
        public bool hasBeenSelected;
        public Color pieceSelected;
    
    
        public PhotonView view;
    
    
        void Update()
        {
            if (hasBeenSelected) //TEMPORARY CHECK FOR 'hasBeenSelected'
            {
                Debug.Log("Wowzers");
                render.color = pieceSelected;
            }
        }
    
    
    
        public void selectedPiece()
        {
            if (!hasBeenSelected)
            {
                Debug.Log("Attempted to raise event"); // Tried using raise events, it never worked
                view.RPC("becomeSelected", RpcTarget.AllBuffered, true);
            }
        }
    
    
        [PunRPC]
        public void becomeSelected(bool selected) //IMPORTANT
        {
            Debug.Log("Event called");
            hasBeenSelected = selected;
        }
    }