- Home /
[SOLVED] Need Help "For" loop to execute in specific Intervals
Hello,
I am fairly new to scripting, and am trying to learn as much as possible about the Unity engine. Currently, I am having trouble with the concept of the "For" loop. My goal is to make a game object: "Enemy1"'s hitPoint variable (set to 100) go down 1 number for every execution of the for loop, however, I need it to go down at timed intervals, not all at once. I know that I should use Time.deltaTime, but I am not sure how or where I should put it.. again I am new to scripting. Thank you SO much!
Here is the code currently attached to the Enemy1 object:
(I know its sloppy and contains many unnecessary variables and code. I created them while experimenting with code i've deleted most of the code unrelated to my question. If you need anything else or if i'm not being specific enough, just let me know)
#pragma strict
public static var myLog : String;
public static var hitPoints :int = 100.0;
var youWin : GameObject;
public static var some_Name : String = "Enemy BOSS ";
var boxHealth : GameObject;
internal var forTime : float;
function OnTriggerEnter (other: Collider) {
if (other.tag == "Player"){
Debug.Log("youve Entered my trigger");
boxHealth.GetComponent(GUIText).enabled = true;
//Here's where I am stumped.. I realize I probably messed up horribly..
for (var i; i<100; i += forTime * Time.deltaTime){
forTime = Time.time + forTime;
hitPoints -=1;
}
}
}
function OnTriggerExit (other: Collider){
//boxHealth.SetActive (false);
boxHealth.GetComponent(GUIText).enabled = false;
Debug.Log("You've Exited my Trigger");
}
Answer by DaveA · Jul 31, 2013 at 07:53 AM
Add this line in your for loop:
yield WaitForSeconds (1);
That will 'pause' the loop for 1 second allowing the rest of the program to run, then resume the loop. So it would take 100 seconds to complete.
haha wow, Thank you. worked Perfectly. To think it'd be so simple.
$$anonymous$$y Updated Code:
function OnTriggerEnter (other: Collider) {
if (other.tag == "Player"){
Debug.Log("youve Entered my trigger");
boxHealth.GetComponent(GUIText).enabled = true;
for (var i=0; i<100; i++){
yield WaitForSeconds (1);
hitPoints -=1;
}
Again, Big thanks.
Your answer
Follow this Question
Related Questions
[Solved] Trying to pause a for loop function OnTriggerExit 1 Answer
Update not being called every frame 2 Answers
how to stop every thing and enter into the OnTriggerEnter function? 0 Answers
Health Regeneration 2 Answers
Triggering over time? 1 Answer