Search Unity

Animation NullReferenceException

Discussion in 'Scripting' started by Tempest74, Jul 21, 2017.

  1. Tempest74

    Tempest74

    Joined:
    May 17, 2017
    Posts:
    133
    I post this here because nobody helped me at answer section.And maybe I will explain more clear. I make a game, when my character have full life I play an animation, when it have 3 another animation and so on. But if I have 3 life instead of 4 I take this error when I leave the current scene and I go into another scene:
    NullReferenceException: Object reference not set to an instance of an object
    LifeControler.UpdateHP (Int32 life, System.Boolean& gameOver) (at Assets/Scripts/LifeControler.cs:34)
    PlayerControlle.Start () (at Assets/Scripts/PlayerControlle.cs:41)
    I know how to fix a normal NullReferenceException (you will see some example into scripts below) but this destroy my game. My life system and my game over text is broke. I hope you can help me.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class PlayerControlle : MonoBehaviour {
    8.  
    9.     public int speed;
    10.  
    11.     static public int gold;
    12.     static public int currentLife;
    13.     bool gameOver = false;
    14.  
    15.     public GameObject bolt;
    16.     public GameObject boltUp;
    17.     public GameObject boltSpawnRight;
    18.     public GameObject boltSpawnLeft;
    19.     public GameObject boltSpawnBack;
    20.     public GameObject boltSpawnForward;
    21.     public Text gameOverText;
    22.  
    23.     private float vertical;
    24.     private float horizontal;
    25.     private bool stopInput = false;
    26.  
    27.     private Rigidbody2D rb;
    28.     private Animator animator;
    29.     private LifeControler life;
    30.  
    31.  
    32.  
    33.     void Start () {
    34.         rb = GetComponent<Rigidbody2D> ();
    35.         animator = GetComponent<Animator> ();
    36.         life = GetComponentInChildren<LifeControler> ();
    37.         if (GlobalControl.Instance != null)
    38.             currentLife = GlobalControl.Instance.CurrentLife;
    39.         else if (GlobalControl.Instance == null)
    40.             Debug.Log ("GlobalControl is null");
    41.         life.UpdateHP (currentLife, out gameOver);
    42.         if (gameOverText != null)
    43.             gameOverText.text = " ";
    44.     }
    45.     private void OnDisable()
    46.     {
    47.         GlobalControl.Instance.CurrentLife = currentLife;
    48.     }
    49.     void Update () {
    50.  
    51.         if (horizontal > 0)
    52.         {
    53.             animator.SetTrigger ("playerMoveRight");
    54.        
    55.             if (Input.GetKeyDown (KeyCode.K))
    56.                 Instantiate (bolt, boltSpawnRight.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
    57.         }
    58.         if (horizontal < 0)
    59.         {
    60.            
    61.             animator.SetTrigger ("playerMoveLeft");
    62.             if (Input.GetKeyDown (KeyCode.K))
    63.                 Instantiate (bolt, boltSpawnLeft.transform.position,  Quaternion.Euler(new Vector3(0, 180, 0)));
    64.            
    65.         }
    66.         if (vertical > 0)
    67.         {
    68.            
    69.             animator.SetTrigger ("playerMoveBack");
    70.             if (Input.GetKeyDown (KeyCode.K))
    71.                 Instantiate (boltUp, boltSpawnBack.transform.position,  Quaternion.Euler(new Vector3(0, 0, 0)));
    72.            
    73.         }
    74.         if (vertical < 0)
    75.         {
    76.             animator.SetTrigger ("playerMoveForward");
    77.             if (Input.GetKeyDown (KeyCode.K))
    78.                 Instantiate (boltUp, boltSpawnForward.transform.position,  Quaternion.Euler(new Vector3(180, 0, 0)));
    79.            
    80.         }
    81.         rb.velocity = new Vector2 (horizontal * speed, vertical * speed);
    82.  
    83.         if (vertical == 0 && horizontal == 0)
    84.         {
    85.             animator.SetTrigger ("playerIdle");
    86.             if (Input.GetKeyDown (KeyCode.K))
    87.                 Instantiate (boltUp, boltSpawnForward.transform.position,  Quaternion.Euler(new Vector3(180, 0, 0)));
    88.         }
    89.  
    90.     }
    91.     void FixedUpdate()
    92.     {
    93.         if(!stopInput)
    94.             horizontal = Input.GetAxis ("Horizontal");
    95.         if(!stopInput)
    96.         vertical = Input.GetAxis ("Vertical");
    97.         rb.velocity = new Vector2 (horizontal * speed, vertical * speed);
    98.     }
    99.  
    100.  
    101.     IEnumerator TakeDamage(int damageValue)
    102.     {
    103.         currentLife = currentLife - damageValue;
    104.         life.UpdateHP (currentLife, out gameOver);
    105.         if (gameOver) {
    106.             stopInput = true;
    107.             gameOverText.text = "Game Over" +
    108.                 "\n Wait 5 seconds";
    109.             yield return new WaitForSeconds (5);
    110.             SceneManager.LoadScene (0);
    111.         }
    112.     }
    113.     void OnTriggerEnter2D(Collider2D other)
    114.     {
    115.         if (other.gameObject.CompareTag ("EnemyBolt")) {
    116.             StartCoroutine (TakeDamage (1));
    117.             Destroy (other.gameObject);
    118.  
    119.         }
    120.     }
    121. }
    122.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LifeControler : MonoBehaviour {
    6.  
    7.     private Animator animator;
    8.     private Vector3 position;
    9.  
    10.     void Awake()
    11.     {
    12.         position = transform.position;
    13.     }
    14.  
    15.     void LateUpdate()
    16.     {
    17.         transform.position = position;
    18.     }
    19.     void Start()
    20.     {
    21.         animator = GetComponent<Animator> ();
    22.         if (animator == null)
    23.             Debug.Log ("Animator in LifeController is missing");
    24.        
    25.     }
    26.  
    27.     public bool UpdateHP(int life, out bool gameOver)
    28.     {
    29.         gameOver = false;
    30.         switch (life) {
    31.         case 4:
    32.             return gameOver = false;
    33.         case 3:
    34.             animator.SetTrigger ("3Life");
    35.             return gameOver = false;
    36.         case 2:
    37.             animator.SetTrigger ("2Life");
    38.             return gameOver = false;
    39.         case 1:
    40.             animator.SetTrigger ("1Life");
    41.             return gameOver = false;
    42.         case 0:
    43.             animator.SetTrigger ("Dead");
    44.             return gameOver = true;
    45.         default:
    46.             Debug.Log ("LifeController error");
    47.             return gameOver = true;
    48.  
    49.         }
    50.     }
    51. }
    52.  
     
  2. Tempest74

    Tempest74

    Joined:
    May 17, 2017
    Posts:
    133
    By the way, if you want another information feel free to ask