Hello Guest

Author Topic: Switching Sprite Collections in script  (Read 11699 times)

Jefemcownage

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Switching Sprite Collections in script
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Switching Sprite Collections in script
« Reply #1 on: April 01, 2012, 01:59:22 am »
Which pointer is null? Secondly, from which function are you calling this?

Jefemcownage

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Switching Sprite Collections in script
« Reply #2 on: April 01, 2012, 02:01:54 am »
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"));


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Switching Sprite Collections in script
« Reply #3 on: April 01, 2012, 02:04:48 am »
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?

Jefemcownage

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Switching Sprite Collections in script
« Reply #4 on: April 01, 2012, 02:13:24 am »
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.

Jefemcownage

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Switching Sprite Collections in script
« Reply #5 on: April 01, 2012, 02:19:34 am »
turns out i don't even need to switch. pretty boneheaded move on my part. sorry for the confusion.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Switching Sprite Collections in script
« Reply #6 on: April 01, 2012, 02:28:36 am »
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.

mrkake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Switching Sprite Collections in script
« Reply #7 on: May 24, 2015, 04:50:35 am »
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?

mrkake

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Switching Sprite Collections in script
« Reply #8 on: May 24, 2015, 05:37:41 am »
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. :)