- Home /
End game after 120 seconds HELP
hey guys!
i have to let my sidescroller game end after 120 seconds... for this i want to let a screen pop up and say "game over". the timer with 120seconds works, but i have a few problems... i have 3 scenes, the third one should be the game over screen. how do i make this? i tried to do it with UI - text, but then the text shows the whole time and covers up my timer and i dont know how to fix this :/ next i tried to do this with an empty gameobject, put in a 3d text, deactivated the main camera (because otherwise, idk why, i cant play the game...) and put in a multipurposecamerarig, target is the 3d text. if i deactivate the maincam of the multicamerarig, of course i get "no camera rendering", but if i activate it i cant see my levels that should be played before the screen pops up... i really dont know how to fix up my mess - and yes, i'm a beginner when it comes to unity and scripting.
Answer by K-Anator · May 06, 2019 at 08:00 PM
Here are a couple of simple scripts I currently use that I find quite handy, and with some tweaking I think they might be what you need... First is StartTimer.cs, it waits a set amount of time, then allows my player to move by calling "AllowMovement()" in my movement controller, which just sets a bool to true.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartTimer : MonoBehaviour
{
[SerializeField]
private float startTimer;
private GameObject player;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
StartCoroutine("CountDown");
}
IEnumerator CountDown()
{
yield return new WaitForSeconds(startTimer);
player.GetComponent<MovementController>().AllowMovement();
}
}
The second is LevelRestart.cs, what this one does, is look for the user to press reload, which I have setup as the R key. Then it reloads the current scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelRestart : MonoBehaviour
{
private void Update()
{
if (Input.GetButton("Reload")) { RestartGame(); }
}
void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Hopefully this helps a bit, since I have absolutely no idea how you're handling your game over and such, but I think enumerators and scene manager are gonna be your friends here. I'm not particularly versed in how scene manager works, or enumerators for that matter, so unfortunately without spending more time on it, I won't be able to provide a directly usable code snippet, but this should definitely help.
Your answer
Follow this Question
Related Questions
SpaceShooter Win the game! 3 Answers
High score value remains constant in game over scene, even if player has scored higher. 1 Answer
Countdown and Gameover,Game over based on collider and timer 1 Answer
Ending a game with Points 1 Answer
How to change a point light intensity with an updating float 1 Answer