- Home /
[JavaScript] Making an image sprite change upon button click?
Hello, I'm new to Unity. I've googled many times and can't seem to find the exact answer.
This is my code so far:
var Player_Character: Sprite;
var Chara1: Sprite;
var Chara2: Sprite;
var Chara3: Sprite;
function ClickThis() {
GetComponent(SpriteRenderer).sprite = Chara2;
}
I wanted to make a "Character Select" scene where when I click on a button, the character image will change. My game is a 2D game by the way.
Attached file is how my Character Select screen looks like. Also follow-up question: how will this character change reflect on all scenes? Thank you very much in advance.!
Answer by giulio-pierucci · Feb 10, 2015 at 04:05 PM
For second question: you can use a script that use DontDestoyOnLoad function. With this, the same instance of script will always be present in all scene (with same data).
For first question: you may reference current player sprite texture from buttons, on OnClick (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUp.html), you can change player material texture.
Your answer
Follow this Question
Related Questions
2D Animation with JavaScript (Android). Help! 0 Answers
Converting screen coordinates to world coordinates 1 Answer
Scripts wont work if deactivated then activated? 0 Answers
When i make my character animated, he can only move up, down left and right (2D) (JS) 0 Answers
How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers