How do I access a variable from a different scene using C#?

So far I have two scenes in my game:

  1. the main menu scene
  2. the actual game scene

In the main menu screen I have a button which enables a type of game mode. If enabled, the player won’t be able to save the game (by disabling the save game script).

I want to know how do I access a Boolean in a script in the Main Menu scene from a script in my Game scene so that I can disable my save game script (C# only).

For example: In Main Menu scene I would have:

public static bool hardcoreMode;

void Awake ()
{
           //some code..

           hardcoreMode = false;

           //more code..
}

void OnGUI()
{
            //some code...
    
            if(GUILayout.Button("HardCore Mode"))
                    hardcoreMode = true;

             //more code...
}

So how do I access the Boolean “hardcoreMode” from a script in my Game scene?

(Beginner in Unity and scripting in general)

Use Singletone class

This seems like a setting you want to store throughout the game, including multiple play sessions, for which you can use PlayerPrefs. This is common practice for storing player/game data.

I believe you could make you boolean a static variable by adding static in front of it, or have a manager game object with that variable and dontdestroyonload.

Dontdestroyonload Unity - Scripting API: Object.DontDestroyOnLoad

Static variable