How do you deactivate a static script until a certain key is pressed?

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!

Figured it out, for future reference; turns out that using another scene is the easiest method, just create another scene then under File > Build Settings drag the new scene into the “scenes to build”. Drag the scene to the top of the order.

To load the scene simply create a script that runs on an empty game object, use an if within the update function to control when it gets activated. Once the if statement is met use this code:

SceneManager.LoadScene(1);

And then the actual game will be loaded, you can also reference the scene name directly instead of calling it by its order on the “scenes to build” page if you fancy.

What you need is a ‘vector’ in time. Time.timeSinceLevelLoad would be correct if the game started running right away. It doesn’t. It starts later. When? Well, at that time when you hit space and start the game.


So, create a static float that records the Time.timeSinceLevelLoad, say something like:

timeLaunched = Time.timeSinceLeveLLoad;

in that code where the space key initiates the game.


Now, your formulae involves the difference, something like:

 public static float GetDifficultyPercent() 
    {
         return Mathf.Clamp01( ( ( Time.timeSinceLevelLoad - timeLaunched )  / secondsToMaxDiff);
    }