Question by
$$anonymous$$ · Sep 20, 2016 at 05:40 PM ·
animationanimatordestroy
Script isn't checking animator state
So i have this code that should prevent an obejct from being destroyed by a touch when certain animation is playing, but unity gives me the following error: object reference not set to an instance of an object.
Said object has the respective animator attached.
Code in c#: using UnityEngine; using System.Collections;
public class touchDestructor : MonoBehaviour {
private Animator anim;
void OnMouseDown (){
if (!anim.GetCurrentAnimatorStateInfo (0).IsName ("burnedAnim")) { //This should prevent the object from being destroyed if it's pressed when burnedAnim is playing
anim = GetComponent<Animator> (); //This triggers a specific animation when said object is touched
anim.SetTrigger ("isPressed");
Destroy (this.gameObject, 0.25f);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612093153im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple call animation then destroy? 0 Answers
How to play and hold on a single frame of an animator clip 1 Answer
Why isn't my animation looping? 3 Answers