Hello Guest

Author Topic: tk2dSpriteAnimator become null (sometimes)  (Read 4596 times)

wijesijp

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
tk2dSpriteAnimator become null (sometimes)
« on: October 21, 2013, 12:07:28 pm »
I have following code in my take damage function
Code: [Select]
if (isAlive)
        {
            towerHealth -= damageAmount;
            if (towerHealth <= 0)
            {

                isAlive = false;
                anim.Stop();

            }
        }

This works properly most of the time.
But once in a while I get following error, NullReferenceException: Object reference not set to an instance of an object

I trace the error to anim (tk2dSpriteAnimator) becoming null.
The main game object with the sprite is active, any idea how this could happen?

Any advice on how to fix this?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSpriteAnimator become null (sometimes)
« Reply #1 on: October 21, 2013, 12:25:29 pm »
No idea how this could happen - tk2dSpriteAnimator is a component, add some breakpoints on all your code that destroys components, maybe this is getting destroyed by accident?

wijesijp

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: tk2dSpriteAnimator become null (sometimes)
« Reply #2 on: October 21, 2013, 01:28:44 pm »
tk2dSpriteAnimator is a child object inside the main object.
I never destroy the animator.

if the parent object is active tk2dSpriteAnimator should be there too right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSpriteAnimator become null (sometimes)
« Reply #3 on: October 21, 2013, 01:34:48 pm »
Yup it should. The animator is never destroyed by any tk2d code - what does the scene looks like immedaitely after its gone null? Does the object still exist, or is it just the reference that has gone null?

wijesijp

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: tk2dSpriteAnimator become null (sometimes)
« Reply #4 on: October 21, 2013, 02:11:54 pm »
Seems like the reference that has gone null
to stop the error I am now doing this,

Code: [Select]
anim = gameObject.GetComponentInChildren<tk2dSpriteAnimator>();
the problem is I can't simulate the problem without adding large number of units to the screen. It does not happen if I have few units on the screen

As you can see from the previous code I am in the process of destroying the main game object when the health goes below 0.
what I am doing is playing death animation and at the end I am destroying the gameObject

 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dSpriteAnimator become null (sometimes)
« Reply #5 on: October 21, 2013, 04:03:35 pm »
How are you destroying the gameobject? If you're using Destroy, try DestroyImmediate. Just to rule out more things....