Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NullReferenceException in Ruby's Adventure: 2D Beginner

Discussion in 'Scripting' started by juuustin, Jun 22, 2019.

  1. juuustin

    juuustin

    Joined:
    Jun 22, 2019
    Posts:
    5
    I try to finish the "Ruby's Adventure: 2D Beginner", and when I want to finish "Sprite Animation", there are some errors. I just copy the code from the tutorial and I tried many times but totally don't know how to fix it.
    The error is:
    NullReferenceException: Object reference not set to an instance of an object
    Rubycontroller.Update () (at Assets/Script/Rubycontroller.cs:48)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Rubycontroller : MonoBehaviour
    6. {
    7.     public float speed = 3.0f;
    8.  
    9.     public int maxHealth = 5;
    10.     public float timeInvincible = 2.0f;
    11.  
    12.     public int health { get { return currentHealth; } }
    13.     int currentHealth;
    14.  
    15.     bool isInvincible;
    16.     float invincibleTimer;
    17.  
    18.     Rigidbody2D rigidbody2d;
    19.     Animator animator;
    20.     Vector2 lookDirection = new Vector2(0, -1);
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         rigidbody2d = GetComponent<Rigidbody2D>();
    26.  
    27.         currentHealth = 4;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         float horizontal = Input.GetAxis("Horizontal");
    34.         float vertical = Input.GetAxis("Vertical");
    35.  
    36.         //Vector2 position = rigidbody2d.position;
    37.         //position.x = position.x + speed * horizontal * Time.deltaTime;
    38.         //position.y = position.y + speed * vertical * Time.deltaTime;
    39.  
    40.         Vector2 move = new Vector2(horizontal, vertical);
    41.  
    42.         if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    43.         {
    44.             lookDirection.Set(horizontal, vertical);
    45.             lookDirection.Normalize();
    46.         }
    47.  
    48.         animator.SetFloat("Speed", move.magnitude);
    49.         animator.SetFloat("Look X", lookDirection.x);
    50.         animator.SetFloat("Look Y", lookDirection.y);
    51.        
    52.  
    53.         Vector2 position = rigidbody2d.position;
    54.  
    55.         position = position + move * speed * Time.deltaTime;
    56.  
    57.         rigidbody2d.MovePosition(position);
    58.  
    59.         if (isInvincible)
    60.         {
    61.             invincibleTimer -= Time.deltaTime;
    62.             if (invincibleTimer < 0)
    63.                 isInvincible = false;
    64.         }
    65.  
    66.     }
    67.  
    68.     public void ChangeHealth(int amount)
    69.     {
    70.         if (amount < 0)
    71.         {
    72.             animator.SetTrigger("Hit");
    73.  
    74.             if (isInvincible)
    75.                 return;
    76.  
    77.             isInvincible = true;
    78.             invincibleTimer = timeInvincible;
    79.         }
    80.  
    81.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    82.         Debug.Log(currentHealth + "/" + maxHealth);
    83.     }
    84. }
    85.  
    upload_2019-6-22_20-55-32.png

    Appreciate any help!!
     
  2. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131

    I can't see anywhere where do you get a reference to the animator.
    You have this line at the top :

    Code (csharp):
    1.  
    2. Animator animator;
    3.  
    But you never make a reference to it.
    You can do it in two ways. Depending if the script is attached to a GameObject that have Animator or not.

    You can change the animator declaration to :

    Code (csharp):
    1.  
    2. public Animator animator;
    3.  
    And then in the editor in the inspector to assign the Animator component.
    Or leave it as it is and add in the Start the line :

    Code (csharp):
    1.  
    2. animator = GetComponent<Animator>();
    3.  
     
    thathurtabit likes this.
  3. juuustin

    juuustin

    Joined:
    Jun 22, 2019
    Posts:
    5
    Got it, and it works, thank you very very much!
     
    DubiDuboni likes this.
  4. thathurtabit

    thathurtabit

    Joined:
    Jul 29, 2018
    Posts:
    6

    Thank you!