Search Unity

Help with RubyController on the Ruby's Adventure 2D animating section

Discussion in 'Scripting' started by ScaredPuppo, Aug 31, 2019.

  1. ScaredPuppo

    ScaredPuppo

    Joined:
    Aug 31, 2019
    Posts:
    1
    So I have no idea whats going wrong can someone have a look at this code for the RubyController and let me know where it is going wrong. The error that is showing up is this:
    "NullReferenceException: Object reference not set to an instance of an object RubyController.Update () (at Assets/Scripts/RubyController.cs:43)"
    When I start the game the Bots work properly but Ruby is stuck in the left idle animation and won't respond to key presses.
    Here's my code:
    Code (CSharp):
    1. public class RubyController : MonoBehaviour
    2. {
    3.     public float speed = 3.0f;
    4.    
    5.     public int maxHealth = 5;
    6.     public float timeInvincible = 2.0f;
    7.    
    8.     public int health {  get { return currentHealth; }}
    9.     public int currentHealth;
    10.     bool isInvincible;
    11.     float invincibleTimer;
    12.     Animator animator;
    13.     Vector2 lookDirection = new Vector2(0,-1);
    14.    
    15.     Rigidbody2D rigidbody2d;
    16.    
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         rigidbody2d = GetComponent<Rigidbody2D>();
    21.  
    22.         currentHealth = maxHealth;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         float horizontal = Input.GetAxis("Horizontal");
    29.         float vertical = Input.GetAxis("Vertical");
    30.                
    31.         Vector2 move = new Vector2(horizontal, vertical);
    32.        
    33.         if(!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    34.         {
    35.             lookDirection.Set(move.x, move.y);
    36.             lookDirection.Normalize();
    37.         }
    38.        
    39.         animator.SetFloat("Look X", lookDirection.x);
    40.         animator.SetFloat("Look Y", lookDirection.y);
    41.         animator.SetFloat("Speed", move.magnitude);
    42.        
    43.         Vector2 position = rigidbody2d.position;
    44.        
    45.         position.x = position.x + speed * horizontal * Time.deltaTime;
    46.         position.y = position.y + speed * vertical * Time.deltaTime;
    47.         rigidbody2d.MovePosition(position);
    48.  
    49.         if (isInvincible)
    50.         {
    51.             invincibleTimer -= Time.deltaTime;
    52.             if (invincibleTimer < 0)
    53.                 isInvincible = false;
    54.         }
    55.     }
    56.  
    57.     public void ChangeHealth(int amount)
    58.     {
    59.         if (amount < 0)
    60.         {
    61.             if (isInvincible)
    62.                 return;
    63.            
    64.             isInvincible = true;
    65.             invincibleTimer = timeInvincible;
    66.         }
    67.        
    68.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    69.        
    70.         Debug.Log(currentHealth + "/" + maxHealth);
    71.     }
    72. }
    If someone could let me know where it is going wrong that would be fantastic thanks!