- Home /
RPC in unity with Photon not working
I am trying to code an online multiplayer poker game for me and my friends. I am just starting out and wanted to make a button, which, when pressed adds the value a ui- slider to the pot on every client. So I tried sending the pot-integer via RPC and it is just not working even after i have read through what feels like every question to that topic in every forum. thank you in advance for your help, I´ve added my script down below =)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class UIManagement : MonoBehaviourPunCallbacks
{
public Text pot;
public int potInner = 0;
public Slider slider;
public int inputBet;
private PhotonView PV;
// Start is called before the first frame update
void Start()
{
PV = GetComponent<PhotonView>();
pot.text = "Pot: 0";
}
// Update is called once per frame
void Update()
{
inputBet = Mathf.RoundToInt(slider.value);
}
public void HigherPot()
{
potInner = potInner + inputBet;
pot.text = potInner.ToString();
photonView.RPC("HigherOtherPot", RpcTarget.All, pot);
}
[PunRPC]
public void HigherOtherPot(string potvalue)
{
Debug.Log("Finally");
pot.text = potvalue;
}
}
Answer by Captain_Pineapple · Apr 04 at 07:41 AM
Your code is almost correct. You should check what kind of variable you are currently trying to plug into your RPC.
your function definition states string
and you give it a Text
in the call.
best would be to change string
to int
as argument type as this is more friendly regarding message size. For things that get sent often consider to predict what range of numbers are enough for your case - do you need negative numbers? if not use an unsigned integer type. if 65535 numbers is enough use a short
or ushort
and so on. Bandwidth does not grow on trees.
Answer by unity2020477 · Apr 04 at 04:55 PM
Thank you very much for answering that fast. It is really nice when as a beginner you needn't solve those issues on your own. Still after having fixed what you suggested, every time I run the Function "Higher Pot" it get the following error message:
NullReferenceException: Object reference not set to an instance of an object
UIManagement.HigherPot () (at Assets/Scripts/UIManagement.cs:36)
UnityEngine.Events.InvokableCall.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:180)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
I have assigned every variable in the inspector and just don't know how to continue. I hope these beginner questions are not bothering you, but trust me; I really tried to find the solution on my own.
Cheers
No worries, everyone has to start somewhere. Nullreference exceptions are in general not allowed in here but either way:
If you understand the concept of the error message and what a Nullref is about then you wont have to ask about this again: A NullrefException says as you might already know that a variable is null
.
Given this you always have to look through all value typed variables that you encounter in the line that your Exception points to:
UIManagement.HigherPot () (at Assets/Scripts/UIManagement.cs:36)
photonView.RPC("HigherOtherPot", RpcTarget.All, pot);
In this case this would be pot
and photonView
. One of them MUST be null
for this to happen. You can always check for this with a Debug.Log
:
Debug.Log($"Values of my variables:\nPV:{photonView} \npot:{pot}");
Now since pot
is assigned in editor it probably is photonView
which is null. This variable exists implicitly when deriving from MonoBehaviourPunCallbacks
. The issue here is probably that you just don't have a PhotonView attached on the same GameObject.
Thanks I got it working now. And I apologize, didn't know null ref questions weren't allowed; Sorry XDXD
while one can argue if that is a good rule or not it is always a good idea to read the FAQ/Rules when you enter a new forum of any kind. This can be really really helpful to avoid common pitfalls like this topic.
Please also consider marking an answer as correct when it contains the solution to your question so that others know that the question was solved and/or have an easier time to find the correct solution in case there are multiple answers.