Christian Albina
Christian Albina

Reputation: 53

How do you temporarily make a sprite disappear in Unity 3D?

In unity, I've added a 2D sprite which has the skin of a booster for my character's rocket boots. I want to make the 2D sprite appear after pressing space and disappear after 1/4 of a second.

How would I turn on and off the visibility of the thruster sprite from C# code?

Upvotes: 1

Views: 1400

Answers (1)

nbura
nbura

Reputation: 388

You may also want to look into using Coroutines. The idea is that it's a method call which may contain delays or waiting periods before the method is "done". See the linked page for a detailed explanation.

Here is a sample coroutine method:

public IEnumerator ShowBoostersForQuarterSecond() {
    spriteRenderer.enabled = true;           //show
    yield return new WaitForSeconds(0.25f);  //wait
    spriteRenderer.enabled = false;          //hide
}

And you would call it by doing

StartCoroutine(ShowBoostersForQuarterSecond());

Note that you might have to do gameObject.renderer instead of spriteRenderer. Or make a field to hold the Sprite Renderer reference, up to you.

Upvotes: 1

Related Questions