2D Toolkit Forum

2D Toolkit => Support => Topic started by: HHErebus on January 18, 2014, 10:20:55 pm

Title: AnimationCompleteDelegate: Object reference not set
Post by: HHErebus on January 18, 2014, 10:20:55 pm
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:
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
_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 :)
Title: Re: AnimationCompleteDelegate: Object reference not set
Post by: unikronsoftware on January 18, 2014, 11:14:31 pm
_characterSprite.animationCompleteDelegate = null;
bombing out implies _characterSprite is null at that point? Please check the value on that / drop a breakpoint to see whats going on.
Title: Re: AnimationCompleteDelegate: Object reference not set
Post by: HHErebus on January 19, 2014, 02:34:26 pm
Thank you for your answer! I think I found what the problem is.

I followed your suggestion and set up a breakpoint: turns out _characterSprite is null because - you guessed it - there is no actual tk2dAnimatedSprite in my object, it's a tk2dSprite with an animator. I searched for the tk2dAnimatedSprite in the tk2d scripts and it's there, but it's marked as deprecated.

To set the animation up I was following your guide here (http://www.unikronsoftware.com/2dtoolkit/docs/1.92/tutorial/scripting_an_animated_sprite.html), which uses that particular deprecated class, is there an "alternative" delegate that should be used for the same purpose in tk2dSprite? If not, could you give me a pointer regarding how to make the animation default to idle after attacking?
Title: Re: AnimationCompleteDelegate: Object reference not set
Post by: unikronsoftware on January 19, 2014, 03:27:07 pm
You're following the tutorial for tk2d 1.92? I guess you're on a newer version - fine the appropriate docs for your version here: www.2dtoolkit.com/docs
The animation system changed between v1 and 2.
http://www.unikronsoftware.com/2dtoolkit/docs/2.3/tutorial/scripting_a_sprite_animator.html
Title: Re: AnimationCompleteDelegate: Object reference not set
Post by: HHErebus on January 20, 2014, 10:06:29 am
Aaaaand, that solved it! Just FYI, I found the guide through google, I didn't even know about the new version  :P

Thanks a lot for your help, have a great day  :)