Search Unity

Question Nullreference error

Discussion in 'Scripting' started by ThorRM, Dec 5, 2022.

  1. ThorRM

    ThorRM

    Joined:
    Dec 3, 2022
    Posts:
    2
    Hi everyone! I'm trying to learn some C# from some tutorials to make games with Unity.
    I've found a game tutorial but a problem stuck me and I can't fix it, I'm not fully mastered in programming to solve by myself so I'm here to ask for your help.
    I've follow this tutorial from A to Z but probably some informations are missing, I'm stopped on 48:45 after write these codes to make the shoot. Also, the ball is stuck on the floor.
    Before them, the ball moved like in the video.

    Thanks in advance to those who will take the time to read and help me.

    The errors are these:

    - NullReferenceException: Object reference not set to an instance of an object
    Player.Update () (at Assets/Game/Player.cs:54)

    Code (CSharp):
    1. using StarterAssets;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     private StarterAssetsInputs starterAssetsInputs;
    9.     private Animator animator;
    10.     private Ball ballAttachedToPlayer;
    11.     private float timeShot = -1f;
    12.     public const int ANIMATION_LAYER_SHOOT = 1;
    13.  
    14.     public Ball BallAttachedToPlayer { get => ballAttachedToPlayer; set => ballAttachedToPlayer = value; }
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         starterAssetsInputs = GetComponent<StarterAssetsInputs>();
    20.     }
    21.  
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (starterAssetsInputs.shoot)
    27.         {
    28.             starterAssetsInputs.shoot = false;
    29.             timeShot = Time.time;
    30.             animator.Play("Shoot", ANIMATION_LAYER_SHOOT, 0f);
    31.             animator.SetLayerWeight(ANIMATION_LAYER_SHOOT, 1F);
    32.         }
    33.         if (timeShot > 0)
    34.         {
    35.             //shoot ball
    36.             if (ballAttachedToPlayer != null && Time.time - timeShot > 0.2)
    37.             {
    38.                 ballAttachedToPlayer.StickToPlayer = false;
    39.  
    40.                 Rigidbody rigidbody = ballAttachedToPlayer.transform.gameObject.GetComponent<Rigidbody>();
    41.                 rigidbody.AddForce(transform.forward * 20f, ForceMode.Impulse);
    42.  
    43.                 ballAttachedToPlayer = null;
    44.             }
    45.  
    46.             // finish kicking animation
    47.             if (Time.time - timeShot > 0.5)
    48.             {
    49.                 timeShot = -1f;
    50.             }
    51.         }
    52.         else
    53.         {
    54.             animator.SetLayerWeight(ANIMATION_LAYER_SHOOT, Mathf.Lerp(animator.GetLayerWeight(ANIMATION_LAYER_SHOOT), 0f, Time.deltaTime * 10f));
    55.         }
    56.     }    
    57. }
    58.  



    - NullReferenceException: Object reference not set to an instance of an object
    Ball.Update () (at Assets/Ball.cs:41)


    Code (CSharp):
    1. using StarterAssets;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using TMPro;
    5. using UnityEngine;
    6.  
    7. public class Ball : MonoBehaviour
    8. {
    9.     [SerializeField] private Transform transformPlayer;
    10.     [SerializeField] private Transform playerBallPosition;
    11.     private bool stickToPlayer;
    12.     float speed;
    13.     Vector3 previousLocation;
    14.     Player scriptPlayer;
    15.  
    16.     public bool StickToPlayer { get => stickToPlayer; set => stickToPlayer = value; }
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         playerBallPosition = transformPlayer.Find("Geometry").Find("BallLocation");
    22.         scriptPlayer = transformPlayer.GetComponent<Player>();
    23.     }
    24.  
    25.         // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (!stickToPlayer)
    29.         {
    30.             float distanceToPlayer = Vector3.Distance(transformPlayer.position, transform.position);
    31.             if (distanceToPlayer < 0.5)
    32.             {
    33.                 StickToPlayer = true;
    34.                 scriptPlayer.BallAttachedToPlayer = this;
    35.             }
    36.         }
    37.         else
    38.         {
    39.             Vector2 currentLocation = new Vector2(transform.position.x, transform.position.z);
    40.             speed = Vector2.Distance(currentLocation, previousLocation) / Time.deltaTime;
    41.             transform.position = playerBallPosition.position;
    42.             transform.Rotate(new Vector3(transformPlayer.right.x, 0, transformPlayer.right.z), speed, Space.World);
    43.             previousLocation = currentLocation;
    44.         }
    45.  
    46.     }
    47. }
    48.  

     
    Last edited: Dec 5, 2022
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    ThorRM and Kurt-Dekker like this.
  3. ThorRM

    ThorRM

    Joined:
    Dec 3, 2022
    Posts:
    2