How to fade out picture
I am making a "instructions picture", where picture should fade out after starting the scene, atm i have made this
using UnityEngine; using System.Collections;
public class Fade : MonoBehaviour { float duration = 2;
void Update() {
if (Time.time > duration) {
Destroy (gameObject);
}
}
}
and as you can see, it just destroys itself after 2 seconds, but I want to fade it, for example over 1 second. And if you know how to even make it like in flappy bird used to have, that after touching the screen, it fades out, not after 2 seconds. I hope you undrestand what I mean and I hope you can help me with this simple problem. Thank you for helping! :)
You need a Renderer attached to your "Instructions picture" gameobject. If it's a Sprite give it a Sprite Renderer etc.
If you have this line without a Renderer it won't know what to get:
GetComponent<Renderer>
Thank you, I tried to use it with texture, so that was problem, but works now. Thank you very much! :)
Answer by Rajeesh_AR · Mar 15, 2016 at 10:26 AM
Hi @karlkevin ,
Try this,
float fadePerSec = 1.5f;
private void Update() {
var material = GetComponent<Renderer>().material;
var color = material.color;
material.color = new Color(color.r, color.g, color.b, color.a - (fadePerSec * Time.deltaTime));
}
Hey @Rajeesh_AR Thank you for helping, but since im beginner, i dont know what "Renderer" means and i get an error:
$$anonymous$$issingComponentException: There is no 'Renderer' attached to the "Image (4)" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "Image (4)". Or your script needs to check if the component is attached before using it.
I hope you can help me with this little thing also :) Thank you :)