- Home /
How to change sprite using a button?
I'm trying to make a little in game store with skins for my player. I've set up my store it has a bunch of buttons. But i don't know how to write a function for them that makes the sprite on my player change into another sprite. I tried to search for some tutorials on this but i can't find any that work. Thank you if you help me with this issue.
do you still need to make the player save the purchased skins and the skin picked even if he quit the game? if that you are looking for it will need more work. Although I know a tutorial that can $$anonymous$$ch you how to do it.
Could you please link me to that tutorial?
https://www.youtube.com/playlist?list=PLLH3mUGkfFCWCsGUfwL$$anonymous$$nDWdkpQuqW3xa
I guess he starts doing the shop at the 5th video.
Answer by lawrence-parry · Sep 17, 2016 at 12:07 PM
Ok, heres how I would do it. Make a new C# script on the object that you want to change the sprite on. In this case I have named it 'ChangeSprite'
using UnityEngine;
using System.Collections;
public class ChangeSprite : MonoBehaviour {
SpriteRenderer spriteRenderer; //will store sprite renderer
void Start()
{
spriteRenderer = gameObject.GetComponent<SpriteRenderer> (); //get sprite renderer & store it
}
public void change(Sprite differentSprite)
{
spriteRenderer.sprite = differentSprite; //sets sprite renderers sprite
}
}
The gist is that we have created a variable to store our sprite renderer (the component that displays sprites in game) We have set that to the sprite renderer attached to this object. Then we have made a function that takes a sprite as input which is used to set the sprite of the sprite renderer.
Anywho go to your button object. There will be an on click section near the bottom. Set the object on the left to the object whose sprite you are changing and set the dropdown on the right to ChangeSprite > change
Now set the sprite you want to change it to below. This is individual for each button so you can use different buttons to set differnt sprites (thumbnails maybe)
hope this helps :) if you need any more info let me know.
I've used your code, and assigned it to the sprite i want to change and the button, problem is when i run the game and press the button i get "UnassignedReferenceException: The variable spriteRenderer of Sprite Changer has not been assigned" . But i don't see how what it means because everything seems to be assigned? Do you have any idea what is wrong? (also thank you for answering my question)
You need to assign sprite to the OnClick( ) event & do check if you have sprite renderer attached to the gameobject and collider as well.
Are you using a sprite component or are you using a UI image. If you are using a UI image the code will have to be a little different.
Solved! thank you. i just deleted my player prefab and created it again, for some reason it worked
I tried using your code and i assigned it to the sprite and the button and i believe everything should be assigned correctly but when i click the button it's just not doing anything for some reason? do you know what could be happening?
Your answer
Follow this Question
Related Questions
How to Retrieve Amount of Light Hitting Sprite? 1 Answer
How to make 2d sprite tiled? 6 Answers
Display Part of a sprite 0 Answers
Rotate Sprite along shape? 0 Answers