Hi all, I am using the following script to add a set amount of health to the player for 30 seconds.
When I press β2β to test it the script works fine, I see the buff apply, then disappear, however if I run it again the buff applies but never disappears again (its like the timer only functions once)
Also, I cant figure out how to stop this from stacking if I keep triggering it.
Any help on this would be greatly appreciated
using UnityEngine;
using System.Collections;
public class ScriptTempHealthBuff : MonoBehaviour {
ScriptPlayerOneManager script;
public int healthBuffAmount = 30;
public int healthBuffDuration = 5;
// Use this for initialization
void Start () {
script = GameObject.Find("GameObjectPlayerOne").GetComponent<ScriptPlayerOneManager>();
StartCoroutine(playerHealthBuffTimer());
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("2"))
{
script.playerTempAttributes.playerOneTempHealth += healthBuffAmount;
playerHealthBuffTimer();
}
}
// Now run a timer
IEnumerator playerHealthBuffTimer()
{
yield return new WaitForSeconds(healthBuffDuration);
playerHealthBuffUndo();
}
//Now return the value back to original
void playerHealthBuffUndo()
{
script.playerTempAttributes.playerOneTempHealth -= healthBuffAmount;
}
}