- Home /
Making the delay actually work
I have a delay in my code now, but it is not waiting the amounted seconds I had assigned it. What do I do know?
The Codes I added are on lines 11-13 and 51-55
Do I have to add something in the middle or add something else to the IEnumerator?
Here is the Code:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public Transform target;
public float healthBarLength;
IEnumerator Do() {
yield return new WaitForSeconds(2);
}
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
animation["death"].wrapMode = WrapMode.Once;
animation["death"].layer = 1;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box (new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
if(curHealth <= 0){
animation.Play ("death");
Do();
Application.LoadLevel(2);
}
}
}
Comment
Best Answer
Answer by robertbu · May 07, 2013 at 07:31 PM
To execute Do:
StartCoroutine(Do());
And Do() itself:
IEnumerator Do() {
yield return new WaitForSeconds(2);
Application.LoadLevel(2);
}
Your answer
Follow this Question
Related Questions
Player lives script 1 Answer
A node in a childnode? 1 Answer
How to delay a respawn? 1 Answer
Player Respawn Script - Not Working 1 Answer
How would you make your player freeze for a certain amount of time? 2 Answers