- Home /
Change sprite when moving(2D)
I want the player sprite to change when moving the player. It worked when I tried it with a few if statements (If 'S' is pressed change the sprite to something, etc), but when I tried to do it with the Input.GetAxis, it doesn't change the sprite. Here's the code (arrow[] is an array of sprites) :
Good day.
How can we help you if dont show what changeSprite() does...?
Answer by AdityaViaxor · Sep 11, 2018 at 10:31 AM
Hello I have found the problem,
The Problem is
private Sprite "spr" will get only sprite what you actually need is
private SpriteRenderer spr;
void Start() { spr = this.getcomponant();
}
move() {
spr = changeSprite(); }
Thanks, it worked! But, why doesn't it work when I try to change the sprite directly as I initially tried?
sprite store image and not of that object and you changed sprite which i am sure changed if you look into editor but what you needed to do is change the sprite variable of spriteRenderer class of player object. in order to change sprite of any oject you need to access class of that object then change the variable ins$$anonymous$$d of that you were changing sprite which has nothing to do with that object so even if get changed it doesnt reflect on player object. please accept my answer if it helped you.
thank you
Your answer
Follow this Question
Related Questions
Sprite flip movement script not working! HELP! 0 Answers
2D Sprite leaves a trail when moving diagonally 1 Answer
Sprite is not shown moving 1 Answer
How to make sprites in SideScroller move in smooth curves 0 Answers
Moving 2D sprite along a path 0 Answers