Search Unity

Null Reference Exception because of an object of a class

Discussion in 'Scripting' started by HristoMalakov, Aug 18, 2017.

  1. HristoMalakov

    HristoMalakov

    Joined:
    Aug 3, 2017
    Posts:
    15
    Hello everybody,

    So, I was making a game where the player must roll a ball on a platform and if they were to fall, they would get a "Press R to try again" message. After pressing R, the level would restart. However, when I press R, I get a Null Reference Exception:
    "NullReferenceException: Object reference not set to an instance of an object
    PlayerController.LateUpdate () (at Assets/Scripts/PlayerController.cs:39)"

    I double checked every line of code and I cannot find the problem. At the line of code where I get an exception, Im using an object of a class to access one of its functions, the object was already initialised. Would you help me pinpoint the error? Thank you in advance.
    Here's the code:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerController : MonoBehaviour {
    8.  
    9.     private float verticalMov;
    10.     private float horizontalMov;
    11.     public float speed = 250, jumpSpeed = 15.0f, gravity = 10;
    12.     public int score;
    13.     public Text scoreText, winText, tryAgainText;
    14.  
    15.     private Vector3 movement;
    16.     private Rigidbody rb;
    17.     public LoadNewLevel loadNewLevel;
    18.  
    19.  
    20.  
    21.     void Start () {
    22.         rb = GetComponent<Rigidbody>();
    23.         score = 0;
    24.    }
    25.  
    26.    void LateUpdate () {
    27.  
    28.         horizontalMov = Input.GetAxis("Horizontal");
    29.         verticalMov = Input.GetAxis("Vertical");
    30.  
    31.         movement = new Vector3(horizontalMov, 0.0f, verticalMov);
    32.  
    33.         if (Input.GetButtonDown("Jump"))
    34.         {
    35.               //jumpSpeed = 5.0f;
    36.             movement.y = jumpSpeed;
    37.         }
    38.  
    39.         rb.AddForce(movement * speed * Time.deltaTime);
    40.                  
    41.         if (transform.position.y < -10)
    42.         {
    43.             tryAgainText.text = ("Press R to try again");
    44.         }
    45.  
    46.         if (Input.GetKeyDown(KeyCode.R) && transform.position.y < -10)
    47.         {
    48.             loadNewLevel.LoadNextLevel();
    49.         }
    50.  
    51.         if (jumpSpeed > 0)
    52.         {
    53.             jumpSpeed -= gravity * Time.deltaTime;
    54.         }
    55.      
    56.     }
    57.  
    58.     private void OnTriggerEnter(Collider other)
    59.     {
    60.         if (other.gameObject.CompareTag("Coins"))
    61.         {
    62.             other.gameObject.SetActive(false);
    63.             score++;
    64.             scoreText.text = "Score: " + score.ToString();
    65.         }
    66.  
    67.         if (score > 4)
    68.         {
    69.             winText.text = ("YOU WIN!");
    70.         }
    71.     }
    72. }
    73.  
    74.  
    75.  
    76.  
    77.  
    78.  
    79. using System.Collections;
    80. using System.Collections.Generic;
    81. using UnityEngine;
    82. using UnityEngine.SceneManagement;
    83.  
    84. public class LoadNewLevel : MonoBehaviour {
    85.  
    86.    public void LoadNextLevel() {
    87.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    88.    }
    89. }
    90.  
    91.  
    92.  
     
    Last edited: Aug 18, 2017
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Where is your rb initialisation?
     
  3. HristoMalakov

    HristoMalakov

    Joined:
    Aug 3, 2017
    Posts:
    15
    Code (csharp):
    1.  
    2. .  
    3. .
    4.     private Rigidbody rb;
    5.     public LoadNewLevel loadNewLevel;
    6.  
    7.  
    8.  
    9.     void Start () {
    10.         rb = GetComponent<Rigidbody>();
    11.         score = 0;
    12.    }
    13. .
    14. .
    15.    
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Since you didn't post your entire script, it's hard to match up line numbers. Which line is giving you the null error?
     
  5. HristoMalakov

    HristoMalakov

    Joined:
    Aug 3, 2017
    Posts:
    15
    I updated the code to include everything. It's line 48.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    So line 39 is rb.AddForce

    Is your PlayerController script on the same object that has a rigidbody?

    If yes, make sure you don't have another copy of the PlayerController script in your scene.
     
  7. HristoMalakov

    HristoMalakov

    Joined:
    Aug 3, 2017
    Posts:
    15
    There is only one Player Controller script attached to the Player, the one with the rigidbody. There are no empty fields in the Player Controller script in Unity.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    right before your rb.addforce do

    Code (CSharp):
    1. if(rb == null)
    2.    Debug.Log("RB is null", gameObject);
    If that prints out, click on it and see if it's highlighting the proper gameobject.