Hey everyone!
I'm coding a simple animation system for an npc, but I'm having some problems with the animation complete delegate.
My code:
void Update(){
// Is the character facing right?
if (transform.position.x < _lastPosition.x){
_facingRight = false;
}
else if (transform.position.x > _lastPosition.x)
{
_facingRight = true;
}
// Animate walk.
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){
_isWalking = true;
if(!_characterAnimator.IsPlaying("Walk R") || !_characterAnimator.IsPlaying("Walk L")){
_characterAnimator.Play(_facingRight ? "Walk R" : "Walk L");
_weaponAnimator.Play(_facingRight ? "Idle R" : "Idle L");
_characterSprite.animationCompleteDelegate = null;
}
}
// Animate attack.
if (Input.GetButtonDown("Fast Attack") || Input.GetButtonDown("Strong Attack"))
{
if(!_characterAnimator.IsPlaying("Attack R" || !_characterAnimator.IsPlaying("Attack L")){
if (_facingRight){
_characterAnimator.Play("Attack R");
_weaponAnimator.Play("Attack R");
}
else if (!_facingRight){
_characterAnimator.Play("Attack L");
_weaponAnimator.Play("Attack L");
}
_characterSprite.animationCompleteDelegate = HitCompleteDelegate;
}
}
_lastPosition = transform.position;
}
void HitCompleteDelegate(tk2dAnimatedSprite sprite, int clipId)
{
_characterAnimator.Play(_facingRight ? "Idle R" : "Idle L");
_weaponAnimator.Play(_facingRight ? "Idle R" : "Idle L");
}
Whenever I play the game, I get this error:
NullReferenceException: Object reference not set to an instance of an object
Assets.Scripts.Game.Graphics.PlayerAnimation.Update () (at Assets/Scripts/Game/Graphics/PlayerAnimation.cs:85)
With the problematic line being
_characterSprite.animationCompleteDelegate = null;
It seems pretty simple to solve. Yet I've been at this for hours, and the solution still eludes me :/ Looking at the "Scripting an Animated Sprite" page, I don't understand what I'm doing wrong.
Any help will be appreciated, thank you very much