SapphireJones
SapphireJones

Reputation: 43

resetting a script every time its called

I am trying to use this script to set a timer for someone looting a player however i can't get it to reset after i call it a second time but it stores the original value of time left where as i want it to start fresh each time. i am calling it from another script which enables and disables it on the OnTriggerEnter and OnTriggerExit methods

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Changetext : MonoBehaviour {
public float timeLeft = 5;
public Text countdownText;
public float cash = 0;

// Use this for initialization
void start()
{
    timeLeft = 5;
}

void Update()
{
    timeLeft -= Time.deltaTime;
    countdownText.text = ("Time Left = " + timeLeft);

    if (timeLeft <= 0)
    {
        countdownText.text = "You got the cash";
        cash = 1;
    }
}

}

Upvotes: 0

Views: 116

Answers (1)

ryeMoss
ryeMoss

Reputation: 4343

Start() only gets called once in the gameobjects lifetime - so it won't toggle everytime you enable/disable.

You can instead use OnEnable() to reset the timer to 5;

void OnEnable()
{
   timeLeft = 5;
}

(as an FYI, Start() needs a capital 'S')

Upvotes: 3

Related Questions