Search Unity

NullReferenceException; Object reference not set to an instance of an object. Pls help!

Discussion in 'Scripting' started by LoPiL, Feb 27, 2017.

  1. LoPiL

    LoPiL

    Joined:
    Dec 22, 2016
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class Movment : MonoBehaviour {
    8.     public float moveSpeed;
    9.     public float jumpHeight;
    10.     public Rigidbody2D rigidBody2D;
    11.     public Transform groundCheck;
    12.     public float groundCheckRadius;
    13.     public LayerMask whatIsGround;
    14.     private bool grounded;
    15.     private bool doubleJumped;
    16.     public Animator Animator;
    17.     public int ourHealth;
    18.     public int maxHealth = 100;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         rigidBody2D = GetComponent<Rigidbody2D>();
    23.         ourHealth = maxHealth;
    24.  
    25.  
    26.     }
    27.     void FixedUpdate() {
    28.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
    29.         Animator.SetBool ("grounded", grounded);
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update () {
    34.         if (Input.GetKeyDown (KeyCode.Space) && grounded ) {
    35.             rigidBody2D.velocity = new Vector2 (0, jumpHeight);
    36.         }
    37.         if (Input.GetKey (KeyCode.A)) {
    38.  
    39.             rigidBody2D.velocity = new Vector2 (-moveSpeed, 0);      
    40.         }
    41.         if (Input.GetKey (KeyCode.D)) {
    42.             rigidBody2D.velocity = new Vector2 (moveSpeed, 0);
    43.         }
    44.         if (ourHealth > maxHealth) {
    45.             ourHealth = maxHealth;
    46.             }
    47.         if (ourHealth <= 0) {
    48.             Die ();
    49.         }
    50.             }
    51.     void Die() {
    52.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    53.     }
    54. }
    55.  
    I dont know how to fix it pls help!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Paste the error message itself. It includes more information such as line numbers that can point directly to the problem. (We're not going to read through your whole script)
     
    LoPiL likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    OK, I lied, I am going to look at your script. You never set the "Animator" member (the way you set rigidbody2D).

    It's also a bad idea to name a variable the same as its class name, and I'm honestly surprised if it even compiles. A common standard is for the variable to be a lowercase version of the class name, so "public Animator animator;"
     
    LoPiL likes this.
  4. LoPiL

    LoPiL

    Joined:
    Dec 22, 2016
    Posts:
    4
    Thx man, i restart unity and this problem disappeared. Thx for help (if i change class name to "public Animator animator" appear another error: http://imgur.com/a/Kwg14)
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yes, you need to change line 29 to match the new name.
     
  6. LoPiL

    LoPiL

    Joined:
    Dec 22, 2016
    Posts:
    4
    Ok thx i change now.