RPC - Null Reference Exception
Options
I'm trying to make multiplayer game like Air Hockey and I can shoot the ball with master but I can't shoot the ball with client. I've tried the owner transfer but with this solution ball moves awkward and laggy. I'm just found out the RPC and I'm trying to implement it but It gives Null Reference.
Here is my script:
Here is my script:
using System.Collections; using System.Collections.Generic; using Photon.Pun; using UnityEngine; [RequireComponent(typeof(PhotonView))] public class ShootControl : MonoBehaviourPun { [SerializeField] private Color effectColor = Color.white; [SerializeField] private string targetTag = "Ball"; [SerializeField] private bool bindCollision = false; [SerializeField] private float power = 5f; [SerializeField] private float Multiplier { get; set; } = 1; private Rigidbody2D target; private PhotonView pv; void Start() { pv = GetComponent<PhotonView>(); } [PunRPC] public void Shoot(float pushForce) { //effectRenderer.color = effectColor; //Invoke("ResetColor", 0.2f); if (target != null) { target.AddForce(DirectionVector * pushForce * 200); } } private void ResetColor() { //effectRenderer.color = Color.black; } private Vector2 DirectionVector { get { Vector3 direction = transform.position - target.gameObject.transform.position; return -direction.normalized; } } [PunRPC] private void OnTriggerEnter2D(Collider2D collision) { if (!bindCollision && collision.gameObject.CompareTag(targetTag)) { target = collision.gameObject.GetComponent<Rigidbody2D>(); target.GetComponent<PhotonView>().RequestOwnership(); target.GetComponent<PhotonView>().TransferOwnership(GetComponent<PhotonView>().Owner); } } [PunRPC] private void OnTriggerStay2D(Collider2D collision) { if (!bindCollision && collision.gameObject.CompareTag(targetTag)) { target = collision.gameObject.GetComponent<Rigidbody2D>(); } } [PunRPC] private void OnTriggerExit2D(Collider2D collision) { if (!bindCollision && collision.gameObject.CompareTag(targetTag)) { target = null; } } [PunRPC] private void OnCollisionEnter2D(Collision2D collision) { if (bindCollision && collision.gameObject.CompareTag(targetTag)) { target = collision.gameObject.GetComponent<Rigidbody2D>(); } } [PunRPC] private void OnCollisionStay2D(Collision2D collision) { if (bindCollision && collision.gameObject.CompareTag(targetTag)) { target = collision.gameObject.GetComponent<Rigidbody2D>(); } } [PunRPC] private void OnCollisionExit2D(Collision2D collision) { if (bindCollision && collision.gameObject.CompareTag(targetTag)) { target = null; } } void Update() { if (!pv.IsMine) { return; } if (Input.GetKeyDown(KeyCode.Space) && Shootable) { Vector3 pos = transform.position + transform.forward * 2f; Vector3 angle = transform.eulerAngles; pv.RPC("Shoot", RpcTarget.All); } } private bool Shootable { get { return true; } } }
0
Comments
-
Hi @anilsadio,
Thank you for choosing Photon!
What PUN version are you using?
Could you post the whole stack trace of the NullReferenceException?0 -
Hello @JohnTube,
I've figured it out. Thank you for your support! But I need help for new issue. May you interest it
https://forum.photonengine.com/discussion/14878/pun2-problem-at-pushing-the-other-players#latest0 -
How did you solve the issue?0
-
@anilsadio, can you help here?0
-
@facecem, the same questions from JohnTube also apply to your problem. It's very likely different than the one of the original post, so feel free to start a thread.
Any NullReferenceException has a callstack in the logs. To analyze this and debug the case is part of regular C# programming, so ... you may need to learn how to read the errors.0