2D Toolkit Forum
2D Toolkit => Support => Topic started by: wijesijp on October 21, 2013, 12:07:28 pm
-
I have following code in my take damage function
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?
-
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?
-
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?
-
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?
-
Seems like the reference that has gone null
to stop the error I am now doing this,
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
-
How are you destroying the gameobject? If you're using Destroy, try DestroyImmediate. Just to rule out more things....