- Home /
Game Over Screen appears on launch.,Something with my script makes it so that everytime I run it a gameover happens
Hello! I am having an issue with the game over screen of the game I am working on. Whenever I launch the game, the game over screen appears. I have done some trouble shooting, and have figured out that it has something to do with the following script. I do not know why it is happening, but it is. Everything looks fine to me, and the gameOver object (tied to the gameover screen) should be set to false upon launch. One thing to note is that if I launch (on the game over screen) and click the back to menu button, and from the main menu go back into the game, then for whatever reason the game over screen seems to be gone. I apologize if this is long, but can anyone find out what about this script makes it so the game over screen is there on launch? (I switched the gameOver function to the pause menu, and sure enough, the pause menu was open on launch.) Thank you for looking into this. Here is my script (sorry if it's long).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameControl : MonoBehaviour
{
private static GameControl instance;
public GameObject gameOver, shield1, shield2, shield3;
public static int shield;
public Vector2 lastCheckpointPos;
// Start is called before the first frame update
void awake()
{
shield = 3;
shield1.gameObject.SetActive (true);
shield2.gameObject.SetActive (true);
shield3.gameObject.SetActive (true);
gameOver.gameObject.SetActive (false);
Time.timeScale = 1;
}
// Update is called once per frame
void Update()
{
//This is what dictates Max SP
if (shield > 3)
shield = 3;
if (shield < 0)
gameOver.gameObject.SetActive (false);
switch (shield)
{
case 3:
shield1.gameObject.SetActive(true);
shield2.gameObject.SetActive(true);
shield3.gameObject.SetActive(true);
gameOver.gameObject.SetActive(false);
break;
case 2:
shield1.gameObject.SetActive(true);
shield2.gameObject.SetActive(true);
shield3.gameObject.SetActive(false);
gameOver.gameObject.SetActive(false);
break;
case 1:
shield1.gameObject.SetActive(true);
shield2.gameObject.SetActive(false);
shield3.gameObject.SetActive(false);
gameOver.gameObject.SetActive(false);
break;
case 0:
shield1.gameObject.SetActive(false);
shield2.gameObject.SetActive(false);
shield3.gameObject.SetActive(false);
gameOver.gameObject.SetActive(true);
Time.timeScale = 0;
break;
}
}
public void Retry()
{
gameOver.SetActive(false);
Time.timeScale = 1f;
shield = 3;
shield1.gameObject.SetActive(true);
shield2.gameObject.SetActive(true);
shield3.gameObject.SetActive(true);
}
}
Answer by ShadoX · Jun 27, 2020 at 06:25 AM
void awake() needs to be void Awake() with a capital A
I'm not sure why, but it seems to be case sensitive, meaning that awake() method is never called and the shield variable is always set to 0, which is the default value for non-initialized INT variables