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;
}
}
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.
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