Hello Guest

Author Topic: SpriteCollectionData has incorrect value for "Count"  (Read 3840 times)

rxmarcus

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
SpriteCollectionData has incorrect value for "Count"
« on: June 07, 2015, 06:04:32 am »
I'm trying to use the .Count property of several SpriteCollectionData assets, and I've found that some of the counts do not reflect the true number of sprites inside. (So I'm getting out of index errors) I have a few sprite collections where I removed a few sprites, and so I'm guessing that the SpriteCollectionData is not updating that count property when you remove sprites?

You can see here, that during debug the sprite collection is saying it has 5 sprites, in reality you can see it only holds 4. I have other sprite collections that appear to show the correct sprite count as well.
Any idea why this is happening? I'm running Unity 5.0.2f1.



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: SpriteCollectionData has incorrect value for "Count"
« Reply #1 on: June 07, 2015, 12:27:47 pm »
Count returns the number of sprites in a collection, and this includes deleted ones (the sprites are identified by index for speed, and indices must remain constant). Not great for the public API, but this can't change for backwards compatiblity reasons.

In any case, you will need to find the first .Valid sprite. In the event you're picking a random sprite from the collection, modify the loop while until you find the first .Valid one.

rxmarcus

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: SpriteCollectionData has incorrect value for "Count"
« Reply #2 on: June 08, 2015, 04:28:28 pm »
Thanks for explaining it, though that sucks pretty bad that you have to cycle through all the sprites checking for .Valid to find the count of a collection.

At least I understand what is happening now. Thanks