2D Toolkit Forum
2D Toolkit => Support => Topic started by: Jefemcownage on April 01, 2012, 01:44:14 am
-
Hey I'm getting a null pointer every time I try the following:
tk2dSpriteCollectionData data = Resources.Load("UI_Collection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
tk2dSprite sprite = GetComponent<tk2dSprite>();
sprite.SwitchCollectionAndSprite(data,data.GetSpriteIdByName("All_Time_Button_Un"));
I need to change the collection of the sprite to UI_Ccollection. Anybody else run into this?
-
Which pointer is null? Secondly, from which function are you calling this?
-
void SetThisWeekSprite(){
tk2dSpriteCollectionData data = Resources.Load("UI_Collection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
tk2dSprite sprite = GetComponent<tk2dSprite>();
//sprite.collection = data;
sprite.SwitchCollectionAndSprite(data,data.GetSpriteIdByName("All_Time_Button_Un"));
AllTime.GetComponent<tk2dButton>().buttonUpSprite = "All_Time_Button_Un";
AllTime.GetComponent<tk2dButton>().buttonDownSprite = "All_Time_Button_Un";
AllTime.GetComponent<tk2dButton>().buttonPressedSprite = "All_Time_Button_Un";
AllTime.GetComponent<tk2dSprite>().spriteId = sprite.GetSpriteIdByName("All_Time_Button_Un");
ThisWeek.GetComponent<tk2dButton>().buttonUpSprite = "This_Week_Button";
ThisWeek.GetComponent<tk2dButton>().buttonDownSprite = "This_Week_Button";
ThisWeek.GetComponent<tk2dButton>().buttonPressedSprite = "This_Week_Button";
ThisWeek.GetComponent<tk2dSprite>().spriteId = sprite.GetSpriteIdByName("This_Week_Button");
}
That's the entire function.
NullReferenceException: Object reference not set to an instance of an object
GameOverGUI.SetThisWeekSprite () (at Assets/Scripts/GameOverGUI.cs:100)
GameOverGUI.Init (Int32 score, Boolean tf) (at Assets/Scripts/GameOverGUI.cs:78)
The nullReference comes on this line:
sprite.SwitchCollectionAndSprite(data,data.GetSpriteIdByName("All_Time_Button_Un"));
-
Hard to say what exactly is null there, it could either be data or sprite. Can you Debug.Log them to find out which is null?
-
Looks like they both are. I know why the sprite object is (my fault) but i'm not sure why it's not finding the UI_Collection.
-
turns out i don't even need to switch. pretty boneheaded move on my part. sorry for the confusion.
-
About not finding UI_Collection - do you have the data object in a folder called Resources? After you commit the collection for the first time, you should rename this object and move it to subfolder called Resources. Otherwise the collection won't be included in the build, and Resources.Load won't find it.
-
I know this is an old thread, but I just found this thread while looking for an answer. I'm trying to do something similar, change to a new sprite collection but I dont have a reference to the object just know the name.
Currently, my sprite collections are NOT in the resources folder, but when I move them to the resources folder they are no longer editable and a message appears which says move them out of resources to continue.
Can you please elaborate a bit on the correct process to edit the collections? Should my flow be when I need to edit move it out of resources and move it back in?
-
ah, i'm just going to reply and answer my own question.
I found this method documented on the spritecollection help page:
tk2dSystem.LoadResourceByName<tk2dSpriteCollectionData>(string s)
This actually works great, then just check the loadable asset checkbox inside the settings of the sprite collection. So there is no need to move it to the resources folder. :)