- Home /
set a delay and count down before going on with script execution
Hey, i want to set a delay to my script, which loos like this at the moment:
using UnityEngine;
using System.Collections;
using plyBloxKit;
public class NavMeshCheck : MonoBehaviour {
public Transform target;
public GameObject Ampel_Green;
public GameObject Ampel_Red;
public GameObject Attention;
private NavMeshAgent agent;
public bool Path;
public float delay_set = 0.75f;
private float delay = 0.0f;
void Awake() {
Ampel_Red.SetActive(true);
Ampel_Green.SetActive(false);
Attention.SetActive(false);
}
void Update() {
agent = GetComponent<NavMeshAgent>();
NavMeshPath path = new NavMeshPath();
agent.CalculatePath(target.position, path);
if (path.status == NavMeshPathStatus.PathComplete) {
//set delay to the value of delay_set, then count down to 0.0. then do this:
Debug.Log("PathComplete");
Path =true;
Ampel_Green.SetActive(true);
Ampel_Red.SetActive(false);
Attention.SetActive(true);
plyBloxGlobal.Instance.SetVarValue("Puzzle", false);
//GetComponent<Animation>().Play("Ball_Jump");
}
else {
Debug.Log("PathInvalid");
Path =false;
Ampel_Red.SetActive(true);
Ampel_Green.SetActive(false);
plyBloxGlobal.Instance.SetVarValue("Puzzle", true);
Attention.SetActive(false);
}
}
}
as you can see, i have already a float delay and delay_set. and commented already the line where it should appear. could somebody help me with that? thanks!
Comment
Best Answer
Answer by FortisVenaliter · Aug 21, 2015 at 03:58 PM
Yeah, this is relatively straightforward. Basically, if it's less than or equal to zero, you run the code and set it to the delay_set variable (if looping). If it's not less than or equal to zero, then subtract Time.deltaTime from it.
public float delay_set = 5.0f;
void Update(){
if(delay_set <= 0){
//Do Something
Debug.Log ("Do Something");
}else{
delay_set -= Time.deltaTime;
Debug.Log (delay_set);
}
}