- Home /
How to delay function Update execution?
I have a scene where a field of text is to fade in by raising the alpha channel over time. The effect works, but I would like to have it wait for a few seconds before it starts fading in. I know "yield.WaitForSeconds" doesn't work in the update function, and I've read online about Coroutines, but I don't really understand them. How could I incorporate this function into my code?
private var myRenderer : Renderer;
function Start () {
myRenderer = renderer;
yield WaitForSeconds(5);
}
function Update () {
//yield WaitForSeconds(5);
if (myRenderer.material.color.a < 255)
myRenderer.material.color.a += 25.0*Time.deltaTime;
}
Answer by infinitypbr · Oct 22, 2014 at 03:28 PM
I was also confused about coroutines, but is think the name is just confusing. The concept is quite simple.
In your update, call a function -- aka coroutine -- with a float variable like this:
function Update(){
MyFunction(0.5);
}
function MyFunction(delay : float){
yield WaitForSeconds(delay);
// enter your code here
}
A coroutine is just another function. And other functions can happen over time, rather than right away.
It's worth pointing out that you can pass many variables to a function:
function Update(){
BigFunction(0.5, "this is a string", gameObjectVariable, stringVariable, intVariable);
}
function BigFunction(delay : float, newString : String, myObject : GameObject, myString : String, myInt : int){
// now try debug.log to print e data of all the variables passed into this function to e console
}
This is helpful for passing variables to a function in another script.
Thank you, I guess it's not so bad after all. This fixed my problem. c:
Answer by robertbu · Oct 22, 2014 at 03:25 PM
The components of the Color class in unity go from 0.0 to 1.0, not 0 to 255. As for waiting, you can do it like this:
function Start () {
myRenderer = renderer;
yield WaitForSeconds(5);
while (myRenderer.material.color.a < 1.0) {
myRenderer.material.color.a += Time.deltaTime / 2.0;
yield;
}
myRenderer.material.color.a = 1.0;
}
Assuming color.a starts at 0.0, this will take 2.0 seconds to fade it up once the 5 second wait is over.
On the contrary, setting it to 1 only makes the alpha go up to 1. Setting it to 255 makes it appear completely. Also, if the alpha of the object is initially set at zero, no transition will occur at all.
Thank you very much for your advice, however!
On the contrary, setting it to 1 only makes the alpha go up to 1 Setting it to 255 makes it appear completely.
No, you are absolutely wrong here. When color.a == 1.0, it is completely visible. From the reference:
Each color component is a floating point value with a range from 0 to 1.
http://docs.unity3d.com/ScriptReference/Color.html
Also, if the alpha of the object is initially set at zero, no transition will occur at all.
Assu$$anonymous$$g you are trying to bring this texture from not visible at all (alpha of 0) to fully visible (alpha of 1), then this code will take 2.0 seconds to do the job.
Did you try this code?
In this type of code, the alpha & color values are a normalized float from 0.0 to 1.0. (Normalized, if I"m not mistaken, means it's a number that's reduced, through math, to a value between 0 and 1).
Your code could actually use a $$anonymous$$athf function (if you haven't checked those out yet, do -- they're really really REALLY handy and will save you lots of time).
var curAlpha : float = 0.0; // The current alpha value. I keep it in a variable so I can use it on multiple things, such as a UI.Image and UI.Text, two different objects that will require the same alpha
var desAlpha : float = 1.0; // The desired alpha value.
var speed$$anonymous$$od : float = 2.0; // $$anonymous$$ultiplies by Time.deltaTime, so a value of 2 is double time, a value of 0.5 is half time (or two seconds)
function Update(){
if (curAlpha != desAlpha)
{
curAlpha = $$anonymous$$athf.$$anonymous$$oveTowards(curAlpha, desAlpha, Time.deltaTime * speed$$anonymous$$od);
myRenderer.material.color.a = curAlpha;
}
}
In the code above, changing the value of desAlpha will change the value of the alpha on your material. This makes it easy to simply pass in a new value from other scripts as needed.
Your answer
Follow this Question
Related Questions
check alpha of gameObject C# 3 Answers
WaitForSeconds, only pausing once 1 Answer
Make delay for spawn 3 Answers