Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 30, 2020 at 05:42 AM by GriffinRubin for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GriffinRubin · Jan 28, 2020 at 08:32 PM · networkingphotonnullreferenceexceptionrpc

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: alt text

Calling of constructOnlineTeams in the button:

alt text

screen-shot-2020-01-28-at-124231.png (98.1 kB)
screen-shot-2020-01-28-at-124245.png (24.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Captain_Pineapple · Jan 29, 2020 at 10:29 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GriffinRubin · Jan 30, 2020 at 05:39 AM 0
Share

Hi @Captain_Pineapple , thanks for the response! It's actually quite funny that you mentioned that I was not actually calling the RPC, because I used to be! In my haste to adjust the file back from something I was trying before posting the question here, I forgot to put that line back the way it should be. All that to say that the error I sent above is actually with that line being correct! But, your comment about making sure to use PhotonView.Instantiate is, in fact, what I was missing! I was not using that to create a copy of GameStarter (the game object containing the GameStarter.cs component) and that was the problem! Thank you very much for the help, I really appreciate it!

avatar image Captain_Pineapple GriffinRubin · Jan 30, 2020 at 02:52 PM 0
Share

glad to be of help :)

Follow this Question

Answers Answers and Comments

244 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Photon Networking] Passing My Player scripts 'Target' object to the Uncontrolled version of My Player 1 Answer

Photon RPC Call help. 0 Answers

How to sync RPC function Photon Network? 1 Answer

Multiplayer car racing wining senior 0 Answers

Null Pointer at RPC Call Unity Network 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges