Multiplayer client problem

Help me please… When the client want to touch the ball i gets this error :

NullReferenceException: Object reference not set to an instance of an object
Player.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Player.cs:184)

 [ClientRpc]
    void  RpcCmdTop()
    {
        GameObject Ball = Instantiate(BallEject, GameObject.Find("BallPoint").GetComponent<Transform>().transform.position, Quaternion.identity) as GameObject;
        NetworkServer.SpawnWithClientAuthority(Ball, this.connectionToClient);
    }


    private void OnTriggerEnter(Collider other)
    {
     
        if (other.CompareTag("Ball")) // if we collide with the ball
        {
            Vector3 dir = GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position - transform.position; // get the direction to where we want to send the ball
            GameObject.FindWithTag("Ball").gameObject.GetComponent<Rigidbody>().velocity = (dir.normalized * currentShot.hitForce) + new Vector3(0, currentShot.upForce, 0);
            //add force to the ball plus some upward force according to the shot being played

            Vector3 ballDir = GameObject.FindWithTag("Ball").GetComponent<Transform>().transform.position - transform.position; // get the direction of the ball compared to us to know if it is

Your error says line 184, but your code snippet is partial so who knows which line that is.

1 Like

sorry u are right :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Player : NetworkBehaviour
{
    [HideInInspector]
    public GameObject playerTargetInstance;
    [SerializeField]
    private GameObject targetA;
    public Transform aimTarget; // the target where we aim to land the ball
    float speed = 2f; // move speed
    float force = 13; // ball impact force
    public GameObject BallEject;

    public bool hitting; // boolean to know if we are hitting the ball or not

    public Transform ball; // the ball
    Animator animator;

    Vector3 aimTargetInitialPosition; // initial position of the aiming gameObject which is the center of the opposite court

    ShotManager shotManager; // reference to the shotmanager component
    Shot currentShot; // the current shot we are playing to acces it's attributes

    private void Start()
    {
        if (!isLocalPlayer)
        {
            return;
        }else
        {
        playerTargetInstance = Instantiate(targetA);
        animator = GetComponent<Animator>(); // referennce out animator
        aimTargetInitialPosition = aimTarget.position; // initialise the aim position to the center( where we placed it in the editor )
        shotManager = GetComponent<ShotManager>(); // accesing our shot manager component
        currentShot = shotManager.topSpin; // defaulting our current shot as topspin
        }
    }

    void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }else
        {
        if (Input.GetKeyDown(KeyCode.R) && !GameObject.FindGameObjectWithTag("Ball")) //sahnede top yoksa r ye bas cmd top yaratsın
        {
            RpcCmdTop();
        }

        float h = Input.GetAxisRaw("Horizontal"); // get the horizontal axis of the keyboard
        float v = Input.GetAxisRaw("Vertical"); // get the vertical axis of the keyboard

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            animator.SetBool("isRunning",true);
        }
        else if
            (Input.GetKeyUp(KeyCode.UpArrow))
                {
            animator.SetBool("isRunning", false);
            animator.Play("idle");
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            animator.SetBool("isBack", true);
        }
        else if
            (Input.GetKeyUp(KeyCode.DownArrow))
        {
            animator.SetBool("isBack", false);
            animator.Play("idle");
        }


        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            animator.SetBool("isLeft", true);
        }
        else if
            (Input.GetKeyUp(KeyCode.LeftArrow))
        {
            animator.SetBool("isLeft", false);
            animator.Play("idle");
        }


        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            animator.SetBool("isRight", true);
        }
        else if
            (Input.GetKeyUp(KeyCode.RightArrow))
        {
            animator.SetBool("isRight", false);
            animator.Play("idle");
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetBool("isJump", true);
        }
        else if
            (Input.GetKeyUp(KeyCode.Space))
        {
            animator.SetBool("isJump", false);
            animator.Play("idle");
        }



        //.............Hedef atış noktasını belirler.....
        if (Input.GetKeyDown(KeyCode.Q))
        {
            hitting = true; // we are trying to hit the ball and aim where to make it land
            animator.SetBool("isRight", false);
            animator.SetBool("isLeft", false);
            currentShot = shotManager.topSpin; // set our current shot to top spin
        }
        else if (Input.GetKeyUp(KeyCode.Q))
        {
            hitting = false; // we let go of the key so we are not hitting anymore and this
        }                    // is used to alternate between moving the aim target and ourself

        if (Input.GetKeyDown(KeyCode.E))
        {
            hitting = true; // we are trying to hit the ball and aim where to make it land
            currentShot = shotManager.flat; // set our current shot to top spin
        }
        else if (Input.GetKeyUp(KeyCode.E))
        {
            hitting = false;
        }



        if (hitting)  // if we are trying to hit the ball
        {
            animator.SetBool("isRight", false);
            animator.SetBool("isLeft", false);
                GameObject.FindWithTag("Target").GetComponent<Transform>().Translate(new Vector3(h, 0, 0) * speed * 2 * Time.deltaTime); //translate the aiming gameObject on the court horizontallly
        }
      



        if ((h != 0 || v != 0) && !hitting) // if we want to move and we are not hitting the ball
        {
            transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime); // move on the court
        }
        else if (GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position.x >= 4)
        {
            hitting = false;

        }
        else if (GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position.x <= -3.4)
        {
            hitting = false;
        }


        }

    }
       //Top nerede yaratılacak
       [ClientRpc]
    void  RpcCmdTop()
    {
        GameObject Ball = Instantiate(BallEject, GameObject.Find("BallPoint").GetComponent<Transform>().transform.position, Quaternion.identity) as GameObject;
        NetworkServer.SpawnWithClientAuthority(Ball, this.connectionToClient);
    }


    private void OnTriggerEnter(Collider other)
    {
       
        if (other.CompareTag("Ball")) // if we collide with the ball
        {
            Vector3 dir = GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position - transform.position; // get the direction to where we want to send the ball
            GameObject.FindWithTag("Ball").gameObject.GetComponent<Rigidbody>().velocity = (dir.normalized * currentShot.hitForce) + new Vector3(0, currentShot.upForce, 0);
            //add force to the ball plus some upward force according to the shot being played

            Vector3 ballDir = GameObject.FindWithTag("Ball").GetComponent<Transform>().transform.position - transform.position; // get the direction of the ball compared to us to know if it is

            if ( ballDir.y >= 1.2)
               
                {
                animator.Play("kafa");

            } else if (ballDir.x >= 0)                                   // on out right or left side
            {
                animator.Play("sagic");                        // play a forhand animation if the ball is on our right
            }
            else                                                  // otherwise play a backhand animation
            {
                animator.Play("solic");
            }

            GameObject.FindWithTag("Target").GetComponent<Transform>().position = aimTargetInitialPosition; // reset the position of the aiming gameObject to it's original position ( center)
            /* ball.GetComponent<Ball>().hitter = "player";
            ball.GetComponent<Ball>().playing = true;*/

            GameObject.FindWithTag("Ball").GetComponent<Ball>().hitter = "player"; //hitter and playing controller
            GameObject.FindWithTag("Ball").GetComponent<Ball>().playing = true; ;
        }
    }


}

Looks like it’s probably “currentShot” that’s null. But it’s hard to tell because your lines are so long. I would suggest breaking up your lines into smaller pieces so it’s easier to understand what’s going on.

You should put some log statements in so you can check the values of your variables on line 183.

Ok i found it.problem was in line 29 if (!isLocalPlayer) thank you for help

isLocalPlayer is a bool. A bool is a value type. Value types cannot be null. So isLocalPlayer was not what was null in the error you posted.

The error lines up with one of the FindWithTag calls you are assuming aren’t returning null in OnTriggerEnter. FindWithTag is designed to return null when no object with that tag is found. So it is usually a good idea to check the result for null before trying to use it.