Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Tempest74 · Jul 20, 2017 at 10:45 AM · animationerrorscene

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

227 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity, Mechanim And Mixamo 3D Animation: NullReferenceException. 0 Answers

Animation Types Won't Play 0 Answers

Door control coordinates being a jerk 0 Answers

can anyone help me out what is wrong with this code? 1 Answer

Getting this error:BCE0020: An instance of type 'UnityEngine.Animation' is required to access non static member 'Play'. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges