Hello Guest

Author Topic: tk2DAnimatedSprite.color in OnEnable()  (Read 9399 times)

Aubrey

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
tk2DAnimatedSprite.color in OnEnable()
« on: January 31, 2012, 01:59:27 pm »
I'm trying to set the color of a tk2DAnimatedSprite in OnEnable(), and I'm getting this error:


Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
tk2dBaseSprite.SetColors (UnityEngine.Color[] dest) (at Assets/TK2DROOT/tk2d/Sprites/tk2dBaseSprite.cs:169)
tk2dSprite.UpdateColorsImpl () (at Assets/TK2DROOT/tk2d/Sprites/tk2dSprite.cs:71)
tk2dSprite.UpdateColors () (at Assets/TK2DROOT/tk2d/Sprites/tk2dSprite.cs:65)
tk2dBaseSprite.set_color (Color value) (at Assets/TK2DROOT/tk2d/Sprites/tk2dBaseSprite.cs:27)
BaseUnit.OnSpawned () (at Assets/Scripts/Gameplay/Units/BaseUnit.cs:131)

here's the relevant code:
Code: [Select]
public public Renderer sprite;//Points to a child of this object with the sprite on it: i always do this to allow for model orientation independence.
internal tk2dAnimatedSprite anim;

        void Awake () { Init(); }//Call a virtual for inheritance hacking: can't use overrides on any of unity's built in component functions
internal virtual void Init() //Called first time the object is ever run. Only need to be done once.
{
...

anim = sprite.gameObject.GetComponent<tk2dAnimatedSprite>();


}

void OnEnable() { OnSpawned(); }//just call a virtual version
internal virtual void OnSpawned()
{

...

anim.Play(0);
anim.color = Color.white.Transparency(0.5f);//crashes here. The Transparency call just returns the same color, but with the given parameter replacing alpha.

}

Any ideas? I change the color later on (for damage effects), and there doesn't seem to be a problem. I wonder if it's something to do with the mesh data not being set up before this object is setup?

..

I'll try putting my script later in the update order to see if that changes anything.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2DAnimatedSprite.color in OnEnable()
« Reply #1 on: January 31, 2012, 10:15:55 pm »
What version of 2D Toolkit are you running? I just tested this on 1.55 and it seems to work fine, and change color, when called from both Awake and OnEnable.

If you're on 1.55, could you try changing the script execution order so the tk2d ones happen before everything else, especially the one which is causing the issue.

Hope that helps.
Cheers,
unikron

Aubrey

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: tk2DAnimatedSprite.color in OnEnable()
« Reply #2 on: February 02, 2012, 01:42:49 pm »
Ah! Might be that it's out of date. I'll check and give it a go, thanks!