NullRefferenceException at animation
So... I have a NRE (NullRefferenceException ) into my game... I know how to normally fix a NRE but now I don't know how to face with this one. Before showing to you my code I will say what kind of problem I have. When I pass from scene 1 ---> 2 everything is good with my game. When I pass from scene 2 back to 1 after I take some damage my game work fine yea my life system. But when I pass again from scene 1 --->2 I take this error 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:42) This blow my mind because it happen when I pass from scene to scene. Any help? Scripts bellow:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LifeControler : MonoBehaviour {
private Animator animator;
private Vector3 position;
void Awake()
{
position = transform.position;
}
void LateUpdate()
{
transform.position = position;
}
void Start()
{
animator = GetComponent<Animator> ();
if (animator == null)
Debug.Log ("Animator in LifeController is missing");
}
public bool UpdateHP(int life, out bool gameOver)
{
gameOver = false;
switch (life) {
case 4:
return gameOver = false;
case 3:
animator.SetTrigger ("3Life");
return gameOver = false;
case 2:
animator.SetTrigger ("2Life");
return gameOver = false;
case 1:
animator.SetTrigger ("1Life");
return gameOver = false;
case 0:
animator.SetTrigger ("Dead");
return gameOver = true;
default:
Debug.Log ("LifeController error");
return gameOver = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerControlle : MonoBehaviour {
public int speed;
static public int gold;
static public int currentLife;
bool gameOver = false;
public GameObject bolt;
public GameObject boltUp;
public GameObject boltSpawnRight;
public GameObject boltSpawnLeft;
public GameObject boltSpawnBack;
public GameObject boltSpawnForward;
public Text gameOverText;
private float vertical;
private float horizontal;
private bool stopInput = false;
private Rigidbody2D rb;
private Animator animator;
private LifeControler life;
void Start () {
rb = GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator> ();
life = GetComponentInChildren<LifeControler> ();
if (GlobalControl.Instance != null)
currentLife = GlobalControl.Instance.CurrentLife;
else if (GlobalControl.Instance == null)
Debug.Log ("GlobalControl is null");
life.UpdateHP (currentLife, out gameOver);
if (gameOverText != null)
gameOverText.text = " ";
}
private void OnDisable()
{
GlobalControl.Instance.CurrentLife = currentLife;
}
void Update () {
if (horizontal > 0)
{
animator.SetTrigger ("playerMoveRight");
if (Input.GetKeyDown (KeyCode.K))
Instantiate (bolt, boltSpawnRight.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
}
if (horizontal < 0)
{
animator.SetTrigger ("playerMoveLeft");
if (Input.GetKeyDown (KeyCode.K))
Instantiate (bolt, boltSpawnLeft.transform.position, Quaternion.Euler(new Vector3(0, 180, 0)));
}
if (vertical > 0)
{
animator.SetTrigger ("playerMoveBack");
if (Input.GetKeyDown (KeyCode.K))
Instantiate (boltUp, boltSpawnBack.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
}
if (vertical < 0)
{
animator.SetTrigger ("playerMoveForward");
if (Input.GetKeyDown (KeyCode.K))
Instantiate (boltUp, boltSpawnForward.transform.position, Quaternion.Euler(new Vector3(180, 0, 0)));
}
rb.velocity = new Vector2 (horizontal * speed, vertical * speed);
if (vertical == 0 && horizontal == 0)
{
animator.SetTrigger ("playerIdle");
if (Input.GetKeyDown (KeyCode.K))
Instantiate (boltUp, boltSpawnForward.transform.position, Quaternion.Euler(new Vector3(180, 0, 0)));
}
}
void FixedUpdate()
{
if(!stopInput)
horizontal = Input.GetAxis ("Horizontal");
if(!stopInput)
vertical = Input.GetAxis ("Vertical");
rb.velocity = new Vector2 (horizontal * speed, vertical * speed);
}
IEnumerator TakeDamage(int damageValue)
{
currentLife = currentLife - damageValue;
life.UpdateHP (currentLife, out gameOver);
if (gameOver) {
stopInput = true;
gameOverText.text = "Game Over" +
"\n Wait 5 seconds";
yield return new WaitForSeconds (5);
SceneManager.LoadScene (0);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Bolt")) {
StartCoroutine (TakeDamage (1));
Destroy (other.gameObject);
}
}
}
I hope you can help me and thanks a lot.