- Home /
2d javascript How to change a sprite during runtime
I ran into a really basic issue that almost nobody seems to have a solid explanation for; I want to change the sprite of my gameObject based in a script I made in Javascript, but the only code I can find that someone says does that is:
var newSprite : Sprite;
function Start () {
GetComponent(SpriteRenderer).sprite = newSprite;
}
But obviously I want to reference the "walk1" sprite from my assets, so where would that go? In "var newSprite : Sprite;" would my image go where "Sprite" is, & how would I go about writing that?
I'm sorry, I'm a noob >.> any help would be greatly appreciated.
Answer by thePeine · Jun 19, 2017 at 01:04 AM
The best way would be to select your game object that contains this script in the Hierarchy View. Then, in the inspector view, drag your "walk1" sprite from your asset folder so that it's assigned to "newSprite" variable. After that, you should be set.
If you want to see a video explanation, take a look at https://www.youtube.com/watch?v=1EJOYWBcrzQ . At about 4:08 in, he describes the drag / drop variable assignment I'm talking about.
Your answer
Follow this Question
Related Questions
Creation of Edge Colliders at Runtime 0 Answers
Can't Get 2D Sprite to Animate 0 Answers
[JavaScript] Making an image sprite change upon button click? 1 Answer
How expensive is scaling in 2D unity? Is it better to pre-scale? 1 Answer
How to show sprite changes made in a Graphics Editor during runtime in Unity Editor 0 Answers