Sprite Sheet on a Prefab, and changing Sprite after instantiate
I am sure that if I knew the terminology word for what I am trying to do, I would be able to find someone who has already done this.
In the Sprite Editor, you can create multiple sprites from one sprite sheet. But normally I see this as an animator type of thing.
I have a sprite sheet with buttons backgrounds on it in 2 states. (normal/selected).
I have labeled each one as say, red_normal and red_selected
I have created a prefab with said sprite sheet, and I can select the sprite from the list inside Unity
I added a public GameObject ButtonSprites to the script, and dragged the sprite sheet created in the editor onto it. And I can use the target button on it to choose a different sprite, and the first one it creates has that color.
Now, when I instantiate the button, I cannot figure out how to reference a different sprite in that sprite sheet via script.
I tried GetComponent().sprite and it is expecting a sprite, so I need a sprite object to pass to it. However, ButtonSprites["blue_normal"] doesn't work as it's not actually an array. So how do I tell it which one to use.
Have you tried creating a public reference to the sprites you need in your script? Something like the following,
[Header("SPRITE REF")]
public Sprite spriteA;
public Sprite spriteA;
public Sprite spriteA;
private SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer = this.GetComponent<SpriteRenderer>();
}
void Update()
{
spriteRenderer.sprite = spriteA;
}
Apply your sprites from in editor to the script references in inspector. Not sure if this is what you're after, hope it helps
They're not individual sprites though. They are a single PNG with it divided using the Sprite Editor:
Screen Shot of how it is setup: Screenshot
Are you saying that I have to do something like:
public Sprite BlueNormal;
public Sprite BlueSelected;
public Sprite GreenNormal;
public Sprite GreenSelected;
etc.,
and drag the sprite onto each one, and select the sprite for each one?
I'd chop them up into individual sprites if i were you, would make life easier. (In case you're not aware, use slice within sprite editor and type in dimensions of one button sprite) Yes that's what i'm saying. It should work if you drag and drop each into their respective reference slots.
If you do decide to approach it this way, I should point out, when you instantiate you're object that holds this script with sprite references, do it from one that is in the scene with all the sprite refs. Not one that has been turned into a prefab as it may lose it's sprite refs.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create a prefab of custom type 1 Answer
How can i get a script of a non instantiate prefab? 2 Answers
Combining Multiple Sprites 0 Answers
Sprite is disappearing in 2D game. 0 Answers
Help with converting small pseudo script to C# please? 0 Answers