Reputation: 193
I have a script that regenerates player's health after 5 seconds:
float counter;
void Update (){
counter += Time.deltaTime;
if(counter > 30minutes){
curHealth++;
counter = 0.0f; }
if(curHealth > 5)
curHealth = 5;
}
Which is attach to my player's health script. I want to start regenerating my player health back when timer is at 0. But I don't know how. This is my player health script:
//Stats
public int curHealth;
public int maxHealth = 3;
public Collider2D col;
public PlayerHealth playerhealthRef;
public TimeManager countDownTimer;
float counter;
public Animator anima; // drag the panel in here again
private UI_ManagerScripts UIM;
DateTime currentDate;
DateTime oldDate;
private GenerateEnemy generateEnemy = null;
void Start ()
{
curHealth = maxHealth;
currentDate = System.DateTime.Now;
if (countDownTimer != null)
{
// countDownTimer.gameObject.SetActive(false);
countDownTimer.Enable(false);
}
if (GameObject.Find("Spawner"))
{
generateEnemy = GameObject.Find("Spawner").GetComponent<GenerateEnemy>();
}
}
void Update ()
{
counter += Time.deltaTime;
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
if ((curHealth <= 0) && (!countDownTimer.gameObject.activeSelf)) {
Die ();
}
if(counter > 5)
{
curHealth++;
counter = 0.0f;
}
if(curHealth > 3)
curHealth = 3;
}
void Awake()
{
UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts> ();
}
void Die() {
UIM.EnableBoolAnimator(anima);
PlayerPrefs.SetInt("RemainingLives", curHealth);
PlayerPrefs.Save();
if (generateEnemy != null)
{
generateEnemy.DestroyAllBlobs();
}
if (countDownTimer != null)
{
countDownTimer.Enable(true);
}
}
public void Damage(int dmg)
{
curHealth -= dmg;
}
}
And this is my Timer countdown script:
public Text timer;
int minutes = 1;
int seconds = 0;
float miliseconds = 0;
[Range(1, 59)]
public int defaultStartMinutes = 1;
public bool allowTimerRestart = false;
public bool useElapsedTime = true;
private int savedSeconds = -1;
private bool resetTimer = false;
private DateTime centuryBegin = new DateTime(2001, 1, 1);
private float tickPerSecond = 10000000.0f;
public void Enable (bool enable)
{
gameObject.SetActive(enable);
ResetTime(); // force the timer to restart
}
void Awake ()
{
minutes = defaultStartMinutes;
if (PlayerPrefs.HasKey("TimeOnExit"))
{
miliseconds = PlayerPrefs.GetFloat("TimeOnExit");
savedSeconds = (int)miliseconds;
if (useElapsedTime && PlayerPrefs.HasKey("CurrentTime"))
{
int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
int ct = PlayerPrefs.GetInt("CurrentTime", elapsedTicks);
PlayerPrefs.DeleteKey("CurrentTime");
elapsedTicks -= ct;
if (elapsedTicks < miliseconds)
{
miliseconds -= elapsedTicks;
}
else
{
miliseconds = 0;
}
}
minutes = (int)miliseconds / 60;
miliseconds -= (minutes * 60);
seconds = (int)miliseconds;
miliseconds -= seconds;
PlayerPrefs.DeleteKey("TimeOnExit");
}
savedSeconds = 0;
}
public void Update()
{
// count down in seconds
miliseconds += Time.deltaTime;
if (resetTimer)
{
ResetTime();
}
if (miliseconds >= 1.0f)
{
miliseconds -= 1.0f;
if ((seconds > 0) || (minutes > 0))
{
seconds--;
if (seconds < 0)
{
seconds = 59;
minutes--;
}
}
else
{
resetTimer = allowTimerRestart;
}
}
if (seconds != savedSeconds)
{
// Show current time
timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
savedSeconds = seconds;
}
}
void ResetTime()
{
minutes = defaultStartMinutes;
seconds = 0;
savedSeconds = 0;
miliseconds = 1.0f - Time.deltaTime;
resetTimer = false;
}
private void OnApplicationQuit()
{
int numSeconds = ((minutes * 60) + seconds);
if (numSeconds > 0)
{
miliseconds += numSeconds;
PlayerPrefs.SetFloat("TimeOnExit", miliseconds);
if (useElapsedTime)
{
int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
PlayerPrefs.SetInt("CurrentTime", elapsedTicks);
}
}
}
}
Thank you :)
Second edit I tried doing this in my update file:
if (countDownTimer = true)
{
if(counter > 5)
{
curHealth++;
counter = 0.0f;
}
if(curHealth > 3)
curHealth = 3;
}
But I got this error: error CS0029: Cannot implicitly convert type bool' to
TimeManager'.
I just wasn't sure of how to go about doing this.?
Upvotes: 0
Views: 239
Reputation: 11414
Most of your code is useless.
You can use WaitForSeconds(just google it) or you can add LeanTween to your project and use
LeanTweed.DeayedCall(5f, ()=>{ /*code that will be runned after 5 sec will pass*/});
I prefer to use LeanTween.
Upvotes: 0
Reputation: 36
According to your second edit line 1
if (countDownTimer = true)
should be
if(countDownTimer)
as = operator is only to give a value, and for comparison you use ==, or, if the variable is bool, simply if(var) or if(!var)
Upvotes: 1