- Home /
How do i make my player sprite change, when entering a powerup?
I am making a start 2d platformer, actually my first game i got the walking, jumping and collisions working But now when i've got to make powerups i wondered how i make a player sprite change to another in a certain amount of time.
Answer by EDevJogos · Feb 03, 2017 at 06:14 PM
To change the sprite you need the SpriteRenderer, if your script is in the same game object as it, you can call.
GetComponent<SpriteRenderer>().sprite = /*The sprite you want.*/
For the time you can use Coroutines or Invoke.
Coroutine:
private IEnumerator ChangeSprite()
{
yield return new WaitForSeconds(/*Time*/);
//Change sprite.
}
You must call this way:
StartCoroutine(ChangeSprite());
And you need to add using System.Collections;
Invoke:
private void ChangeSprite()
{
//Change Sprite
}
You must call like this: Invoke("ChangeSprite", /*Time*/);
For more info you can check:
https://docs.unity3d.com/Manual/Coroutines.html
or
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
Your answer
Follow this Question
Related Questions
Help! How can I get my power up script to work? 1 Answer
How do I change a gameobjects jump int (height) 1 Answer
Unity 2D - Yoshi styled "PowerUp"-Creature 1 Answer
Particle system activate onTriggerEnter2D 2 Answers
Poor quality sprite 2D game 3 Answers