I’m trying to have a piece of code run when a ball hits a trigger(to finish the stage) in an attempt to get the timer to stop running and add it to a score.
public class Finish_Cube : MonoBehaviour
public bool levelFin = false;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
print("Level complete!");
levelFin = true;
}
}
public class Timer : MonoBehaviour
Finish_Cube finishCube;
public int time;
public bool _running;
public Text timerText, failText;
public int timeToScore;
private void Awake()
{
finishCube = GameObject.Find("FinishGameObject").GetComponent<Finish_Cube>();
}
void Start()
{
_running = true;
StartCoroutine(countdown());
failText.text = "";
}
IEnumerator countdown()
{
while (time > 0)
{
yield return new WaitForSeconds(1);
time -= 1;
}
failText.text = "LEVEL FAIL";
_running = false;
}
private void Update()
{
if (finishCube.levelFin) //here
{
StopAllCoroutines();
timeToScore = time;
print(timeToScore);
}
timerText.text = "Time: " + time.ToString();
}