- Home /
Render enable for 5 seconds?
What I have:
using UnityEngine;
using System.Collections;
public class Locked : MonoBehaviour
{
void Update(){
float seconds = Time.time;
bool s= seconds % 2==0;
renderer.enabled= s;
}
void Main(){
renderer.enabled= Start.Locked;
}
}
Start.Locked:
public static bool Locked;
Basically, I want the object to have its render enabled for like 5 seconds, but I have no idea how to do this... please help THANKS!!
What drive the turning on and off? Given this code, it looks like you might want to add the turning on and off in the Start script so that Start.Locked changes value. Or do you want Start.Locked changed in this script? Or do you want to bypass Start.Locked to disable/enable this setting?
One method to disable this setting is to use Invoke() on a method that turns the renderer off. For example:
void RendOff() {
renderer.enabled = false;
}
Then you can do:
Invoke("RendOff", 5.0f);
Answer by cdrandin · Apr 02, 2013 at 11:14 PM
Basic timer implementation not calling the expensive Co-routines or Invoke
float timer;
int waitingTime;
void Update(){
timer += Time.deltaTime;
if(timer > waitingTime){
//Action
timer = 0;
}
}
Does some actions every waitingTime seconds.
Thanks! basically, I worked based on your text and it still didnt seem to work, soon I noticed I had only the sign wrong -.-
using UnityEngine;
using System.Collections;
public class Locked : $$anonymous$$onoBehaviour {
void Update(){
if (Start.timer < 5){
Start.timer += Time.deltaTime;
renderer.enabled = true;
}else{
renderer.enabled = false;
}
}
}
this is what it all came down to if anyone else comes here with the same problem :)
Answer by MorphingDragon · Apr 02, 2013 at 11:32 PM
C# co-routines should be able do this quite easily.
public IEnumerator MyCoroutine(Object value)
{
// Do some things
yield return new WaitForSeconds(5);
// Do some more things 5 seconds later
}
To start a coroutine use the MonoBehaviour.StartCoroutine(MethodName, ArgValue) Method.
http://docs.unity3d.com/Documentation/ScriptReference/index.Writing_Scripts_in_Csharp_26_Boo.html
Your answer
Follow this Question
Related Questions
How to work a real life timer? 2 Answers
How to make a Time Based Score? 1 Answer
Game Timer help needed 2 Answers
How Do I check if a Bool was True a few moments ago 2 Answers
Timer doesn't work properly 2 Answers