Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

"NullRefenenceException: Object reference not set to an instance of an object"

Discussion in 'Documentation' started by Debhole, Nov 10, 2017.

  1. Debhole

    Debhole

    Joined:
    Nov 10, 2017
    Posts:
    1
    So I'm trying to make it when your character stands on a certain thing, in this case a yellow pad with the tag jumpboost, your character gets some affect. There isn't any syntax error but when I start the game it spams in the console "NullReferenceException: Object reference not set to an instance of an object PlayerMotor.GiveJumpBoost () (at Assets/Scripts/PlayerMotor.cs:66)" Can anyone help me? The error is on line 66.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Rigidbody))]
    5. [RequireComponent(typeof(GameObject))]
    6. public class PlayerMotor : MonoBehaviour {
    7.  
    8.     private Vector3 velocity = Vector3.zero;
    9.  
    10.     [SerializeField]
    11.     private float jumpSpeed = 8f;
    12.  
    13.     public float jumpBoostStrength = 10f;
    14.     public float speedBoostStregth = 8f;
    15.  
    16.     private Rigidbody rb;
    17.  
    18.     void Start()
    19.     {
    20.         rb = GetComponent<Rigidbody>();
    21.     }
    22.     //Gets a movement vector
    23.     public void Move(Vector3 _velocity)
    24.     {
    25.         velocity = _velocity;
    26.     }
    27.  
    28.     //Run every physics iteration
    29.     private void FixedUpdate()
    30.     {
    31.         PerformMovement();
    32.         PerformJump();
    33.         GiveJumpBoost();
    34.     }
    35.  
    36.     //Perform movement based on velocity variable
    37.     void PerformMovement()
    38.     {
    39.         if (velocity != Vector3.zero)                                   //Time.fixedDeltaTime is the time between each frame
    40.         {                                                               //This prevents someone from moving faster on higher fps
    41.             rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
    42.         }
    43.     }
    44.  
    45.     //Jump based on a velocity variable
    46.     void PerformJump()
    47.     {
    48.         //Vector3 variable
    49.         Vector3 dwn = transform.TransformDirection(Vector3.down);
    50.  
    51.         if (Physics.Raycast(transform.position, dwn, 0.6f))
    52.         {
    53.             if (Input.GetButtonDown("Jump"))
    54.                 rb.velocity = new Vector3(0, jumpSpeed, 0);
    55.         }
    56.     }
    57.  
    58.     void GiveJumpBoost()
    59.     {
    60.         Vector3 dwn2 = transform.TransformDirection(Vector3.down);
    61.  
    62.         RaycastHit rayHit = new RaycastHit();
    63.         if (Physics.Raycast(transform.position, dwn2, 1f))
    64.         {
    65.             Collider raycastHitCollider = rayHit.collider;
    66.             if (raycastHitCollider.gameObject.tag == "jumpboost")
    67.             {
    68.                 Debug.Log("JUUMP!!!!");
    69.             }
    70.         }
    71.     }
    72. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287