2D Toolkit Forum
2D Toolkit => Support => Topic started by: eklavyaa on January 05, 2015, 09:35:26 am
-
Hello,
I am trying to change the sprite collection at run time as follows :-
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
collectionInst = collection.inst;
in following function
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 ?
-
have you tried printing out BearAnimation.m_Collection?
-
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
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
-
So the line Resources.Load(BasePath.m_BasePathForCollections+"Hyderate" , typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
crashes the build? Have you looked at the build logs?
-
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 :-
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
-
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.