2D Toolkit Forum

2D Toolkit => Support => Topic started by: eklavyaa on January 05, 2015, 09:35:26 am

Title: Crash on changing sprite collection at runtime.
Post by: eklavyaa on January 05, 2015, 09:35:26 am
Hello,

I am trying to change the sprite collection at run time  as follows :-

Code: [Select]
BearAnimation.m_Collection = Resources.Load(BasePath.m_BasePathForCollections+"Hyderate" , typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
BearAnimation.m_AnimationObject.GetComponent<tk2dBaseSprite>().SetSprite(BearAnimation.m_Collection,1);

but if give following error :-

Object reference not found at  line

Code: [Select]
collectionInst = collection.inst;
in following function

Code: [Select]
public void SetSprite(tk2dSpriteCollectionData newCollection, int newSpriteId) {
bool switchedCollection = false;
if (Collection != newCollection) {
collection = newCollection;
collectionInst = collection.inst;
_spriteId = -1; // force an update, but only when the collection has changed
switchedCollection = true;
}

it essentially means there is no sprite collection already set , which is not true as I have attached one.


Please suggest how I can swap sprite collections at run time ?
Title: Re: Crash on changing sprite collection at runtime.
Post by: unikronsoftware on January 05, 2015, 10:25:32 am
have you tried printing out BearAnimation.m_Collection?
Title: Re: Crash on changing sprite collection at runtime.
Post by: eklavyaa on January 05, 2015, 12:36:31 pm
Quote
have you tried printing out BearAnimation.m_Collection?

It won't give anything as by default its null and while loading collection to it crashes the build.

so I just tried printing the

Code: [Select]
BearAnimation.m_AnimationObject.GetComponent<tk2dBaseSprite>().Collection.name
as I am trying to change the collection of BearAnimation.m_AnimationObject and it gives name correct name.

Please let me know if I missed anything


Title: Re: Crash on changing sprite collection at runtime.
Post by: unikronsoftware on January 05, 2015, 12:58:46 pm
So the line Resources.Load(BasePath.m_BasePathForCollections+"Hyderate" , typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
crashes the build? Have you looked at the build logs?
Title: Re: Crash on changing sprite collection at runtime.
Post by: eklavyaa on January 06, 2015, 05:15:05 am
Code: [Select]
Resources.Load(BasePath.m_BasePathForCollections+"Hyderate" , typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
this line works fine and I tried printing name of collection after this line like :-

Code: [Select]
Debug.Log("----- BearAnimation.m_Collection "+BearAnimation.m_Collection.name);
it crashes here also saying :-

NullReferenceException: Object reference not set to an instance of an object

so if I try to set this sprite  collection it gives the crash as I have mentioned earlier.

PS : I am using unity 4.6.1
Title: Re: Crash on changing sprite collection at runtime.
Post by: unikronsoftware on January 06, 2015, 01:21:07 pm
That's not a crash, its an exception saying exactly that the error says - Resources.Load is returning null. You need to figure out why that is the case, maybe you've not put it in a resources folder, maybe the object name is incorrect.