- Home /
Problem is not reproducible or outdated
Game Over Canvas not showing
I think I messed something up.
I'm pulling my hair out working on this. When the allotted time counts down and reaches zero, the TimeOver canvas does not show up. The same thing is happening to the GameOver canvas each time the player is out of lives. Here is the code for both.
CountdownTimer.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
public class CountdownTimer : MonoBehaviour
{
public static float startingTime = 45f;
public static float countingTime;
public Text theText;
public GameObject outOfTime;
public HealthManager theHealth;
public CameraController cam;
public UnitManager um;
private AudioSource source1;
public GameObject gameOver;
public AudioClip bat;
public AudioClip bell;
public AudioClip net;
public bool isPaused;
public bool timerActive = true;
public PlayerController ball;
public GameStatus gs;
public int moveOnDelay = 3;
void Start()
{
outOfTime.gameObject.SetActive(false);
countingTime = startingTime;
theHealth = FindObjectOfType<HealthManager>();
source1 = GetComponent<AudioSource>();
um = FindObjectOfType<UnitManager>();
ball = FindObjectOfType<PlayerController>();
gs = FindObjectOfType<GameStatus>();
}
void Update()
{
if (isPaused)
{
return;
}
if (timerActive)
{
countingTime -= Time.deltaTime;
theText.text = "Time: " + Mathf.Round(countingTime).ToString("f0");
if (countingTime <= 10f)
{
TimeRunningOut();
}
if (countingTime <= 0f)
{
TimeUp();
}
}
}
public void ResetTime()
{
countingTime = startingTime;
theText.color = Color.blue;
}
public void TimeRunningOut()
{
theText.color = Color.red;
}
public void UpdateScore()
{
if (countingTime <= 10f)
{
gs.AddScore(500);
}
else
{
int timeScore = (int)countingTime * 2;
gs.AddScore(timeScore);
}
ResetTime();
timerActive = false;
}
public void TimeUp()
{
if (countingTime <= 0f)
{
countingTime = 0f;
theHealth.DestroyPlayer();
if (GameStatus.lives > 1 && !theHealth.isDead)
{
StartCoroutine("TimeOverCo");
}
if (GameStatus.lives == 1 && !theHealth.isDead)
{
gameOver.gameObject.SetActive(true);
}
}
}
public IEnumerator TimeOverCo()
{
outOfTime.gameObject.SetActive(true);
yield return new WaitForSeconds(3);
outOfTime.gameObject.SetActive(false);
ResetTime();
}
}
GameOver.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameOver : MonoBehaviour
{
public float waitAfterGameOver = 10f;
public Text txt;
public UnitManager um;
private CountdownTimer ct;
public GameObject gameOver;
public PlayerController ball;
// Start is called before the first frame update
void Start()
{
um = FindObjectOfType<UnitManager>();
ct = FindObjectOfType<CountdownTimer>();
gameOver.gameObject.SetActive(false);
}
public void GameOverScreen()
{
gameOver.gameObject.SetActive(true);
waitAfterGameOver -= Time.deltaTime;
if (waitAfterGameOver <= 0)
{
um.StartOver();
}
txt.text = "Continue? " + Mathf.Round(waitAfterGameOver);
txt.color = Color.green;
}
public void Quit()
{
Application.Quit();
}
}
What should I do to make both panels work?
Answer by privatecontractor · Jan 23 at 05:56 AM
Hi @burchland2 ,
Man inside this part try to add:
theHealth.DestroyPlayer();
if (GameStatus.lives > 1 && !theHealth.isDead)
{
**Debug.Log("Made condition to call: TimeOverCo");**
StartCoroutine("TimeOverCo");
}
if (GameStatus.lives == 1 && !theHealth.isDead)
{
**Debug.Log("Made condition to call: GameOver.StaActive(true);**
gameOver.gameObject.SetActive(true);
}
So you will see is this problem with conditions which not ocurre or with voids/coorutines called...
Also you write that time is running, so if conditions are not checked what is doing void: theHealth.DestroyPlayer();
probably seeting some bool like isDead to true, check this out... maybe you changing private bool and not updating public etc... or maybe you check is:
!theHealth.isDead
(beacuse you checking for false value...) so he isAlive ?
theHealth.isDead
So lets startr with Debug.Log() and look why...
Hope will do, if you will still strugling provide feedback, and will look some more.
Thank you very much for your help! I think I'm finished with the base portion of the game now. Again, I appreciate your assistance.
Sincerely, burchland2