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: