Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by AryaIsMe · Nov 24, 2021 at 05:30 PM · 3dnullreferenceexceptionanimator controller

Why does this work but this doesn't even though they are the same thing?

In my game I am trying to trigger the death animation but it doesn't work when I use anim.SetBool("isDead", true); however when I use GetComponent().SetBool("isDead", true); it works. I initialized anim by doing this: private Animator anim; and in Start anim = this.gameObject.GetComponent();. Then I did the previous code and only the getcomponenet works. When I do anim.SetBool("isDead", true); it gives an NullRefrenceExeption Errror. The isDead bool is a actual variable in the animator.

This is the script: (The important parts will have comments beside them saying important):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class Enemy
 {
     public float health;
     public float speed;
     public NavMeshAgent agent;
     public float inRange;
     public float inView;
     
 
     public Enemy(float health, float speed, NavMeshAgent agent, float inRange, float inView)
     {
         this.health = health;
         this.speed = speed;
         this.agent = agent;
         this.inRange = inRange;
         this.inView = inView;
         
     }
 }
 
 public class EnemyBaseScript : MonoBehaviour
 {
     public float health;
     public float speed;
     public NavMeshAgent agent;
     public float inRange;
     public float inView;    
     public Enemy enemy;
     public GameObject self;
     public bool MultiAttack = false;
 
     public GameObject playerPos;
 
     private float SelectedDif;
         
     private Animator anim; //important
 
     private PlayerPos pl;
     private Weapon weaponGettingHitBy;
     private TakeDamage tk;
     private TimeToFight TtF;
     private Difficulty dif;
     private GameObject buttonController;
 
     // Start is called before the first frame update
     void Start()
     {
         pl = GetComponent<PlayerPos>();
         tk = GetComponent<TakeDamage>();
         TtF = GetComponent<TimeToFight>();
         dif = GetComponent<Difficulty>();
         buttonController = GameObject.FindGameObjectWithTag("ButtonController");
 
         SelectedDif = buttonController.GetComponent<Difficulty>().difficulty;
         SceneManager.UnloadSceneAsync("DifficultyChosing");
 
         health = health * SelectedDif;
         speed = speed * SelectedDif;
         inRange = inRange * SelectedDif;
         inView = inView * SelectedDif;
         
 
         enemy = new Enemy(health, speed, agent, inRange, inView);
         agent = gameObject.GetComponent<NavMeshAgent>();
         self = this.gameObject;
 
         anim = this.gameObject.GetComponent<Animator>(); //important
     }
 
     public void Update()
     {
         Invoke("LookForPlayer", 5.0f);
        
         if (PlayerPos.FindPlayerPos(inRange, playerPos, inView, self))
         {
            TimeToFight.ChoseChasingWhatStateToAttackPlayer(agent, playerPos, self, anim, MultiAttack);           
         }
         
         if (health < 0.1f)
         {
             StartCoroutine(JustDies());            
         }
     }    
         
     IEnumerator JustDies() //important
     {
         //anim.SetBool("isDead", true);
         GetComponent<Animator>().SetBool("isDead", true);
 
         yield return new WaitForSeconds(4.5f);
         Destroy(this.gameObject);
     }
 
     public GameObject LookForPlayer()
     {
         playerPos = GameObject.FindGameObjectWithTag("Player");
         return playerPos;
     }
 
     private void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Weapon")
         {
             weaponGettingHitBy = new Weapon(col.gameObject, col.gameObject.GetComponent<WeaponBaseScript>().damagedealing);
             health = TakeDamage.TakeDmg(health, weaponGettingHitBy);
         }
     }
 }
 
Comment
Add comment · Show 1
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
avatar image CodesCove · Nov 24, 2021 at 08:19 PM 0
Share

null error basically means that it didn't find the Animator component at the Start. Are you perhaps adding it later? Try make it public and set the reference manually in the inspector.

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

172 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

Related Questions

How do I activate an animation when a gameobject enter the Collider of the other animated object 2 Answers

How to fix multiple character animations playing at the same time during a run animation? 1 Answer

Root Movement with Additive Layers 0 Answers

Vector3 TransformDirection "object reference"-error 2 Answers

Drag Object Via Axis Handles 0 Answers


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