2D Toolkit Forum
2D Toolkit => Support => Topic started by: Vartib on December 13, 2013, 12:42:46 am
-
The current script I'm working on is going to be used to swap out sprites for my terrain objects during runtime, but I've run into a problem with retrieving the sprite IDs. My goal is to have a single script do the swapping for all of my terrain objects so I don't have to have a script attached to each of them individually. From what I've read in the documentation it should be as easy as doing the following:
public GameObject ParentTile;
private tk2dSprite sprite;
private int spriteID;
sprite = ParentTile.GetComponent<tk2dSprite>();
spriteID = sprite.GetSpriteIdByName("RoomFloor0");
ParentTile is an object I add to the script in the editor that has a sprite with the correct sprite collection selected. Everything looks like it should work, but when I run the program I get the following error at the line with "spriteID = ..."
"NullReferenceException: Object reference not set to an instance of an object."
It seems like whatever is getting saved as "sprite" isn't what the program wants, but I'm at a loss as to what it's looking for.
-
What does the parent tile game object contain?
-
Sorry, should have included that in the original post. Here's a screenshot: http://i.imgur.com/b3pyZGK.png
-
Thats quite weird, the sprite seems to have a null mesh filter. It shouldn't. Can you try to create a repro case and email support at unikronsoftware.com? That would make it a lot easier in tracking this down.
-
Don't have time to email it today, but I wanted to add that the Mesh Filter seems to change to "[none]" when I drag the tile from the Hierarchy to the project assets, which is where I normally store the parent tiles so they don't have to be in the scene directly. When I move it back to the Hierarchy it goes back to normal. Am I not supposed to store "parent" objects as assets like this?
-
No thats expected. Your screenshot didn't have any context in there so I had no idea what was going on.
I'm running out of things to suggest - can you create a repro case for this? If possible email it to support and I'll be happy to investigate this.
-
Just to be sure there's closer with this "issue," I wanted to update that this ended up being a problem with the user :D I'll spare you the details haha.
Thanks for your time!