what is wrong in this code-:
public int delay;
void Update() {
GetComponenet<Image>().color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
if(Time.deltaTime >= delay * Time.deltaTime)
{
return;
}
}
what is wrong in this code-:
public int delay;
void Update() {
GetComponenet<Image>().color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
if(Time.deltaTime >= delay * Time.deltaTime)
{
return;
}
}
add these into your script
public bool updateOn = true;
void Start ()
{
StartCoroutine (updateOff());
}
void Update ()
{
if (updateOn == true)
{
//whatever you want update to do.
}
// if you want certain parts of update to work at all times write them here.
}
IEnumerator updateOff ()
{
yield return new WaitForSeconds (5.0f);
updateOn = false;
}
You need to make sure to increment a counter with new values. I re purposed your delay to be my counter and hard coded the 5 seconds.
Time.deltaTime is the time since last frame, not since starting.
float delay = 0;
void Update()
{
if (delay > 5.0f)
{
return;
} else
{
delay += Time.deltaTime;
}
}
Hi Abhi94,
You need to be using Time.time or Time.realtimeSinceStartup instead of Time.deltaTime in your if statement. Time.deltaTime only measures the time since the previous frame, not the total time elapsed since the game started.
Assuming that delay is the max time you want the Update() function to work for, your if statement could look like this:
if(Time.time >= delay){
return;
}
You probably want this at the very start of your Update() function.
I hope this helps,
Rich
bool stop = false;
void Start ()
{
Invoke ("ToggleStopOn ", 5);
}
void ToggleStopOn ()
{
stop = true;
}
void Update()
{
if (stop)
return;
GetComponenet<Image>().color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
}
Just don’t use the Update if you need to stop it after some time.
void Update(){
if(timer < time){}
}
Once the timer is off, the update is still run and the check is still done even though we do not care about it anymore.
Just use a coroutine that you run for the needed time
void Start(){
StartCoroutine(MyUpdate())
}
IEnumerator MyUpdate()
{
float timer = 0f;
float time = 5f;
while(timer < time){
timer += Time.deltaTime;
yield return null;
}
// Here do anything that needs be done after 5s
}
This won’t take any resources after 5s. Also, you may destroy the component/object if what it contains is useless after that.