2D Toolkit Forum
2D Toolkit => Support => Topic started by: Eldh on September 07, 2012, 11:40:03 am
-
Hi,
So i instantiate a prefab, in this when i change the spriteID, randomly in my case, the polygon collider won't update. It stay the same since the instantiate.
Is there a way to update the collider when i change the sprite ?
-
It doesn't for a good reason, but it isn't convenient like this. I am aware of it and it is likely to change in a future update.
The easiest way around this is to create a new sprite using the sprite collection from the prefab - create a GameObject, and use tk2dSprite.AddComponent(...) to add a sprite to the gameObject. If you really need the prefab, destroy the attached collider before switching. I think that should sort it out. The reason it isn't doing this automatically is that this is a very costly process in terms of CPU usage, so its best not to do it that often.
-
Mhh i tried to use tk2dSprite.AddComponent(...) on an empty gameobject like you said but it does not create a collider at all...
I instantiate my prefab which contain several sprites, then i add the tk2dsprite component to the gameObject (in my prefab) cause only this need to change randomly, but i'm relatively a beginner so maybe i do something wrong ? all this happens on Start.
-
I'll look into this for you - I'm just recovering from a HD crash, so it'll likely be tomorrow before I get a chance to investigate this.
-
Ahaa. I found the issue.
When creating a sprite using tk2dSprite.AddComponent(...), the collider isn't created. Change tk2dBaseSprite.CreateCollider like so -
public static T AddComponent<T>(GameObject go, tk2dSpriteCollectionData spriteCollection, int spriteId) where T : tk2dBaseSprite
{
T sprite = go.AddComponent<T>();
sprite._spriteId = -1;
sprite.collection = spriteCollection;
sprite.spriteId = spriteId;
sprite.Build();
return sprite;
}
Now it should work correctly.
-
it's working, thx !