Search Unity

Multiplayer client problem

Discussion in 'Scripting' started by ltnksr, May 14, 2020.

  1. ltnksr

    ltnksr

    Joined:
    Oct 4, 2017
    Posts:
    7
    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)


    Code (CSharp):
    1.  [ClientRpc]
    2.     void  RpcCmdTop()
    3.     {
    4.         GameObject Ball = Instantiate(BallEject, GameObject.Find("BallPoint").GetComponent<Transform>().transform.position, Quaternion.identity) as GameObject;
    5.         NetworkServer.SpawnWithClientAuthority(Ball, this.connectionToClient);
    6.     }
    7.  
    8.  
    9.     private void OnTriggerEnter(Collider other)
    10.     {
    11.      
    12.         if (other.CompareTag("Ball")) // if we collide with the ball
    13.         {
    14.             Vector3 dir = GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position - transform.position; // get the direction to where we want to send the ball
    15.             GameObject.FindWithTag("Ball").gameObject.GetComponent<Rigidbody>().velocity = (dir.normalized * currentShot.hitForce) + new Vector3(0, currentShot.upForce, 0);
    16.             //add force to the ball plus some upward force according to the shot being played
    17.  
    18.             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
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Your error says line 184, but your code snippet is partial so who knows which line that is.
     
    ltnksr likes this.
  3. ltnksr

    ltnksr

    Joined:
    Oct 4, 2017
    Posts:
    7
    sorry u are right :)
     
  4. ltnksr

    ltnksr

    Joined:
    Oct 4, 2017
    Posts:
    7
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class Player : NetworkBehaviour
    7. {
    8.     [HideInInspector]
    9.     public GameObject playerTargetInstance;
    10.     [SerializeField]
    11.     private GameObject targetA;
    12.     public Transform aimTarget; // the target where we aim to land the ball
    13.     float speed = 2f; // move speed
    14.     float force = 13; // ball impact force
    15.     public GameObject BallEject;
    16.  
    17.     public bool hitting; // boolean to know if we are hitting the ball or not
    18.  
    19.     public Transform ball; // the ball
    20.     Animator animator;
    21.  
    22.     Vector3 aimTargetInitialPosition; // initial position of the aiming gameObject which is the center of the opposite court
    23.  
    24.     ShotManager shotManager; // reference to the shotmanager component
    25.     Shot currentShot; // the current shot we are playing to acces it's attributes
    26.  
    27.     private void Start()
    28.     {
    29.         if (!isLocalPlayer)
    30.         {
    31.             return;
    32.         }else
    33.         {
    34.         playerTargetInstance = Instantiate(targetA);
    35.         animator = GetComponent<Animator>(); // referennce out animator
    36.         aimTargetInitialPosition = aimTarget.position; // initialise the aim position to the center( where we placed it in the editor )
    37.         shotManager = GetComponent<ShotManager>(); // accesing our shot manager component
    38.         currentShot = shotManager.topSpin; // defaulting our current shot as topspin
    39.         }
    40.     }
    41.  
    42.     void Update()
    43.     {
    44.         if (!isLocalPlayer)
    45.         {
    46.             return;
    47.         }else
    48.         {
    49.         if (Input.GetKeyDown(KeyCode.R) && !GameObject.FindGameObjectWithTag("Ball")) //sahnede top yoksa r ye bas cmd top yaratsın
    50.         {
    51.             RpcCmdTop();
    52.         }
    53.  
    54.         float h = Input.GetAxisRaw("Horizontal"); // get the horizontal axis of the keyboard
    55.         float v = Input.GetAxisRaw("Vertical"); // get the vertical axis of the keyboard
    56.  
    57.         if (Input.GetKeyDown(KeyCode.UpArrow))
    58.         {
    59.             animator.SetBool("isRunning",true);
    60.         }
    61.         else if
    62.             (Input.GetKeyUp(KeyCode.UpArrow))
    63.                 {
    64.             animator.SetBool("isRunning", false);
    65.             animator.Play("idle");
    66.         }
    67.  
    68.         if (Input.GetKeyDown(KeyCode.DownArrow))
    69.         {
    70.             animator.SetBool("isBack", true);
    71.         }
    72.         else if
    73.             (Input.GetKeyUp(KeyCode.DownArrow))
    74.         {
    75.             animator.SetBool("isBack", false);
    76.             animator.Play("idle");
    77.         }
    78.  
    79.  
    80.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    81.         {
    82.             animator.SetBool("isLeft", true);
    83.         }
    84.         else if
    85.             (Input.GetKeyUp(KeyCode.LeftArrow))
    86.         {
    87.             animator.SetBool("isLeft", false);
    88.             animator.Play("idle");
    89.         }
    90.  
    91.  
    92.         if (Input.GetKeyDown(KeyCode.RightArrow))
    93.         {
    94.             animator.SetBool("isRight", true);
    95.         }
    96.         else if
    97.             (Input.GetKeyUp(KeyCode.RightArrow))
    98.         {
    99.             animator.SetBool("isRight", false);
    100.             animator.Play("idle");
    101.         }
    102.  
    103.         if (Input.GetKeyDown(KeyCode.Space))
    104.         {
    105.             animator.SetBool("isJump", true);
    106.         }
    107.         else if
    108.             (Input.GetKeyUp(KeyCode.Space))
    109.         {
    110.             animator.SetBool("isJump", false);
    111.             animator.Play("idle");
    112.         }
    113.  
    114.  
    115.  
    116.         //.............Hedef atış noktasını belirler.....
    117.         if (Input.GetKeyDown(KeyCode.Q))
    118.         {
    119.             hitting = true; // we are trying to hit the ball and aim where to make it land
    120.             animator.SetBool("isRight", false);
    121.             animator.SetBool("isLeft", false);
    122.             currentShot = shotManager.topSpin; // set our current shot to top spin
    123.         }
    124.         else if (Input.GetKeyUp(KeyCode.Q))
    125.         {
    126.             hitting = false; // we let go of the key so we are not hitting anymore and this
    127.         }                    // is used to alternate between moving the aim target and ourself
    128.  
    129.         if (Input.GetKeyDown(KeyCode.E))
    130.         {
    131.             hitting = true; // we are trying to hit the ball and aim where to make it land
    132.             currentShot = shotManager.flat; // set our current shot to top spin
    133.         }
    134.         else if (Input.GetKeyUp(KeyCode.E))
    135.         {
    136.             hitting = false;
    137.         }
    138.  
    139.  
    140.  
    141.         if (hitting)  // if we are trying to hit the ball
    142.         {
    143.             animator.SetBool("isRight", false);
    144.             animator.SetBool("isLeft", false);
    145.                 GameObject.FindWithTag("Target").GetComponent<Transform>().Translate(new Vector3(h, 0, 0) * speed * 2 * Time.deltaTime); //translate the aiming gameObject on the court horizontallly
    146.         }
    147.      
    148.  
    149.  
    150.  
    151.         if ((h != 0 || v != 0) && !hitting) // if we want to move and we are not hitting the ball
    152.         {
    153.             transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime); // move on the court
    154.         }
    155.         else if (GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position.x >= 4)
    156.         {
    157.             hitting = false;
    158.  
    159.         }
    160.         else if (GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position.x <= -3.4)
    161.         {
    162.             hitting = false;
    163.         }
    164.  
    165.  
    166.         }
    167.  
    168.     }
    169.        //Top nerede yaratılacak
    170.        [ClientRpc]
    171.     void  RpcCmdTop()
    172.     {
    173.         GameObject Ball = Instantiate(BallEject, GameObject.Find("BallPoint").GetComponent<Transform>().transform.position, Quaternion.identity) as GameObject;
    174.         NetworkServer.SpawnWithClientAuthority(Ball, this.connectionToClient);
    175.     }
    176.  
    177.  
    178.     private void OnTriggerEnter(Collider other)
    179.     {
    180.        
    181.         if (other.CompareTag("Ball")) // if we collide with the ball
    182.         {
    183.             Vector3 dir = GameObject.FindWithTag("Target").GetComponent<Transform>().transform.position - transform.position; // get the direction to where we want to send the ball
    184.             GameObject.FindWithTag("Ball").gameObject.GetComponent<Rigidbody>().velocity = (dir.normalized * currentShot.hitForce) + new Vector3(0, currentShot.upForce, 0);
    185.             //add force to the ball plus some upward force according to the shot being played
    186.  
    187.             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
    188.  
    189.             if ( ballDir.y >= 1.2)
    190.                
    191.                 {
    192.                 animator.Play("kafa");
    193.  
    194.             } else if (ballDir.x >= 0)                                   // on out right or left side
    195.             {
    196.                 animator.Play("sagic");                        // play a forhand animation if the ball is on our right
    197.             }
    198.             else                                                  // otherwise play a backhand animation
    199.             {
    200.                 animator.Play("solic");
    201.             }
    202.  
    203.             GameObject.FindWithTag("Target").GetComponent<Transform>().position = aimTargetInitialPosition; // reset the position of the aiming gameObject to it's original position ( center)
    204.             /* ball.GetComponent<Ball>().hitter = "player";
    205.             ball.GetComponent<Ball>().playing = true;*/
    206.  
    207.             GameObject.FindWithTag("Ball").GetComponent<Ball>().hitter = "player"; //hitter and playing controller
    208.             GameObject.FindWithTag("Ball").GetComponent<Ball>().playing = true; ;
    209.         }
    210.     }
    211.  
    212.  
    213. }
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    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.
     
  6. ltnksr

    ltnksr

    Joined:
    Oct 4, 2017
    Posts:
    7
    Ok i found it.problem was in line 29 if (!isLocalPlayer) thank you for help
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.