Hello Guest

Author Topic: Dynamic Change of Collection  (Read 4801 times)

hromoydron

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 28
    • View Profile
Dynamic Change of Collection
« on: August 18, 2013, 01:04:09 pm »
I want to change SpriteCollection while game is playing.

I try simply change name    
Code: [Select]
currentIcon.GetComponent<tk2dSprite>().Collection.name=warriorName+"SpriteCollection";But nothing has changed.

Than I try this method:
Code: [Select]
currentIcon.GetComponent<tk2dSprite>().Collection= (tk2dSpriteCollectionData)Resources.Load(warriorName+"SpriteCollection");
When I make resources.load my collection should be in the folder Resources without other folders between collection and Resourse folder, yes? (I made like this)

But, has got an error :NullReferenceException: Object reference not set to an instance of an object
tk2dBaseSprite.set_Collection (.tk2dSpriteCollectionData value) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dBaseSprite.cs:51)


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Change of Collection
« Reply #1 on: August 18, 2013, 01:35:25 pm »
The second one is almost correct. You need the sprite collection data object (the one inside the Data folder, and just that, nothing else). You should then be able to load it with Resources.Load. Make sure resources.load is actually loading first though, so print out the value. For obvious reasons if its null it isn't going to work.

Also, use SetSprite to switch to a sprite from the new collection, rather than using .Collection to assign it.
Eg. SetSprite( newCollection, "spriteName" );

hromoydron

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Dynamic Change of Collection
« Reply #2 on: August 18, 2013, 09:03:33 pm »
I make like this:

Code: [Select]
tk2dSpriteCollectionData currentCollectionData=(tk2dSpriteCollectionData)Resources.Load("bombermanSpriteCollection");
Debug.Log(currentCollectionData);

 "bombermanSpriteCollection" is a correct name. But currentData=null. Why?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamic Change of Collection
« Reply #3 on: August 18, 2013, 09:10:36 pm »
Try this:
tk2dSpriteCollectionData currentCollectionData=(tk2dSpriteCollectionData)Resources.Load("bombermanSpriteCollection", typeof(tk2dSpriteCollectionData));

Also its in a resources folder, right? Its not going to work otherwise.

hromoydron

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Dynamic Change of Collection
« Reply #4 on: August 18, 2013, 09:27:58 pm »
Oh, I wrote Resource - not Resources. Sorry!!!

But without your additions (typeof(tk2dSpriteCollectionData)) it does't work. So thank you!