Forum Thread
[UNITY C#] Changing the sprite of an object depending on its stats (Forums : Coding & Scripting : [UNITY C#] Changing the sprite of an object depending on its stats) Post Reply
Thread Options
Jan 19 2017 Anchor

Hello everybody!

I am planning to make a game where the player has to sort boxes in a shelf.
The boxes are randomly generated, each of them can have 1 out of 5 possible shapes, can contain either food or non food goods, can be heavy or light, etc.

Now i would like the box to look different for each combination, is there a way to build up the sprite by adding small elements, like labels on the box?

Or should all the sprites be made beforehand, one for each possible combination?

If it is possible, could you give me an idea of the logic?

Thanks in advance! Smiley

Apr 21 2017 Anchor

I believe you would have to build each individual Sprite. Logic wise you would write a script that makes a new Sprite object, gets the Sprite image component, rolls a random number between whatever bounds you want, and finally use an if statement to assign whatever image you want depending on what it's rolls.

Unfortunately I can't go into a lot of detail as I'm pretty new to unity. Just my 2 cents, hope it helps!

May 9 2017 Anchor

It sounds like you're describing something like paperdolling :)

Here's how I'd do it:

Have a sprite for each so-called 'small element'.

Have an Empty GameObject for each 'box' that will act as the parent to all of the combined 'small elements'. (we can move this one around, and all others will move relative to it)

Have a prefab GameObject for each 'small element' (using the relevant sprite)

In a script (or scripts, however you want to do it), instantiate the 'small element' prefab as and when you need to, and make it a child of the correct 'box' GameObject (in other words, make the 'box' the parent object).

And make sure the positions of the prefab are behind/ontop of the box to make sure you can actually see the small elements!

Let me know if I can go into any more detail for you or if you think you get what I mean ^_^

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.