Hello Guest

Author Topic: Dynamic Sprite Creation problem  (Read 4648 times)

corndog16

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Dynamic Sprite Creation problem
« on: December 15, 2012, 09:26:54 pm »
I am attempting to cause an icon to appear above a characters head when the character is clicked.
To do this I am attempting to create a sprite dynamically. but I'm having some issues.

I have read this forum post: http://unikronsoftware.com/2dtoolkit/forum/index.php?topic=36.0
and I am attempting to use the following code:
Code: [Select]
tk2dSpriteCollectionData sc = Resources.Load("HeroCollection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
if (sc != null)
{;
GameObject go = new GameObject();
tk2dSprite sprite = go.AddComponent<tk2dSprite>();
sprite.SwitchCollectionAndSprite(sc, 1);
sprite.Build();
sprite.scale = new Vector3(1300, 1300, 2);
}

The problem is that, while "HeroCollection" is the name of the sprite collection I wish to use, The tk2dSpriteCollectionData object is always null. I assume this is because the Resources.Load call fails.
What am I missing here?

Thanks.

nikolic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Dynamic Sprite Creation problem
« Reply #1 on: December 15, 2012, 10:53:41 pm »
You have to move sprite collection data file into \Resources folder in the project hierarchy in order to make this work.

I suggest you create a prefab for the character head and instantiate it in start() method of a script. You can initially move the sprite off-screen and simply change its position when needed.

If there are more character heads in the scene, I suggest you use the same prefab, instantiate the sprite and set spriteID for each character in start() method.