Photon RPC Parameter NullReferenceException

Hi there, sorry if the solution to this question is obvious. I’m very new to Photon and am trying to figure out the basics. I’d like to make an RPC method with a supplied integer parameter from a button, but for some reason no matter what I do I get a NullReferenceException on the parameter. I’ve verified multiple times that the parameter’s value is indeed being set (via a Debug.Log statement), but nonetheless as soon as I try the RPC it tells me NullReferenceException. At the moment the code is a bit wonky because I thought that I needed a PhotonView component on the GameObject that has the script with the RPC method, but that hasn’t fixed anything. Any help would be appreciated!


Class containing the RPC:

public class GameStarter : MonoBehaviour, IPunObservable
{
    private GlobalScript glob;
    private int numberOfPlayers = 0;

    void Start()
    {
        glob = GameObject.Find("GlobalObject").GetComponent<GlobalScript>();
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(numberOfPlayers);
        }
        else
        {
            // Network player, receive data
            this.numberOfPlayers = (int)stream.ReceiveNext();
        }
    }

    public void sendInfo(int numPlayers)
    {
        numberOfPlayers = numPlayers;
        sendGameInfoToGlob();
    }

    [PunRPC]
    public void sendGameInfoToGlob()
    {
        glob.startMultiplayerGame(numberOfPlayers);
    }
}

Method called by button:

public void constructOnlineTeams(int numPlayers)
	{ 
	    loading.SetActive(true);
		Debug.Log("number of players: " + numPlayers);
		gameStarter.GetComponent<GameStarter>().sendInfo(numPlayers);
    }

Method referenced in the RPC:

public void startMultiplayerGame(int numPlayers)
    {
        PhotonNetwork.LoadLevel("MainMenu");
        main = GameObject.Find("MainMenuController").GetComponent<MainMenuScript>();
        main.setUpTeams(numPlayers);
    }

**Error: **

NullReferenceException: Object reference not set to an instance of an object
GlobalScript.startMultiplayerGame (System.Int32 numPlayers) (at Assets/Scripts/MainGame/GlobalScript.cs:129)
GameStarter.sendGameInfoToGlob () (at Assets/Scripts/MainGame/GameStarter.cs:38)
GameStarter.sendInfo (System.Int32 numPlayers) (at Assets/Scripts/MainGame/GameStarter.cs:32)
GameManager.constructOnlineTeams (System.Int32 numPlayers) (at Assets/Scripts/MainGame/GameManager.cs:104)
UnityEngine.Events.InvokableCall1[T1].Invoke (T1 args0) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:207)
UnityEngine.Events.CachedInvokableCall1[T].Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:345)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:70)
UnityEngine.UI.Button.Press () (at /Applications/Unity/Hub/Editor/2019.2.17f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Applications/Unity/Hub/Editor/2019.2.17f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Applications/Unity/Hub/Editor/2019.2.17f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at /Applications/Unity/Hub/Editor/2019.2.17f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at /Applications/Unity/Hub/Editor/2019.2.17f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

GameObject containing GameStarter.cs:

Calling of constructOnlineTeams in the button:

151962-screen-shot-2020-01-28-at-124245.png

Hey there,

no worries, a lot of people struggle with this at first. You currently do not call a RPC properly in your current code.

Currently you are just calling a function. Just adding [PunRPC] does not make it a message sent to someone else.
What you need to do to actually send something is the following:

photonView.RPC("[YOURMETHODNAMEHERE]", PhotonTargets.[WHOAREYOUSENDINGTO?], [HEREYOURARGUMENTS]);

this will call upon an attached photonView on the current object assuming that it is derived from MonoBehaviourPun instead of Monobehaviour and are using PUN2. (If you are not using PUN2 then come back to this at state it in a comment)

assuming that you instantiated the given object with the attached photonview in a Photon.Instantiate call this object should exist for every player in your game. This again will mean that the rpc call will automaticaly search for the image of the object with the photonview upon which it was called and execute the function with the given arguments there.

A way to check if the message will end up at the right target is to check the photon id given to the photonView. They should be “related” between the same object in all game instances.

let me know if that helped you, if something was unclear just ask.