Hey there, I have a problem in that the game I’m making has a “start screen”, which basically disables game objects until a certain key is pressed, hence starting the game. The code to do this looks like so:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadScreen : MonoBehaviour {
public GameObject startScreen;
public GameObject player;
public GameObject gameOverManager;
public GameObject blockSpawner;
bool waitingToStartGame = true;
void Start () {
if (startScreen != null)
{
startScreen.SetActive(true);
}
else
{
waitingToStartGame = false;
Debug.LogError("Start screen has not been set in the inspector, please assign the start screen UI and try again.");
}
if (player != null)
{
player.SetActive(false);
}
else
{
Debug.LogError("Player has not been set in the inspector, please assign the start screen UI and try again.");
}
if (gameOverManager != null)
{
gameOverManager.SetActive(false);
}
else
{
Debug.LogError("Game over manager has not been set in the inspector, please assign the start screen UI and try again.");
}
if (blockSpawner != null)
{
blockSpawner.SetActive(false);
}
else
{
Debug.LogError("blockspawner has not been set in the inspector, please assign the start screen UI and try again.");
}
}
void Update () {
if (waitingToStartGame && (Input.GetKeyDown(KeyCode.Space)))
{
waitingToStartGame = false;
if (startScreen != null)
{
startScreen.SetActive(false);
}
if (player != null)
{
player.SetActive(true);
}
if (gameOverManager != null)
{
gameOverManager.SetActive(true);
}
if (blockSpawner != null)
{
blockSpawner.SetActive(true);
}
}
}
}
The problem is that I have a static piece of code that is acting as a difficulty control, it has a function that is recording the time passed since the scene is loaded, the problem is that it starts counting up as soon as the game loads, meaning when the player actually starts the game by pressing “space”, the difficulty has already incremented by (x) amount.
Here is the code from the static class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Difficulty {
static float secondsToMaxDiff = 90;
public static float GetDifficultyPercent() {
//return 1;
return Mathf.Clamp01(Time.timeSinceLevelLoad / secondsToMaxDiff);
}
}
How can I go about fixing this? Will I need to create a seperate scene that contains the “start screen”, and then once space is pressed it loads the actual game? Or is there a way to make the current setup work?
Thank you in advance for any help!