Globally accessible GameState manager

Hey guys,

I’m trying to create a global script to manage game states, but even if i set a specific gamestate it wont change for some reason.

This is the gamestate script:

using UnityEngine;
using System.Collections;

public static class GameState {
	public enum States {Running, Died , Pause , Won}
	public static States state = States.Pause;
	
	public static bool Running {
		get { 
			switch(state){
			case States.Running:
				return true;
			}
			return false;
		}
		set {
			if(value == true)
			state = States.Running;
		}
	}
	public static bool Died {
		get { 
			switch(state){
			case States.Died:
				return true;
			}
			return false;
		}
		set {
			if(value == true)
			state = States.Died;
		}
	}
	public static bool Pause {
		get { 
			switch(state){
			case States.Pause:
				return true;
			}
			return false;
		}
		set {
			if(value == true)
			state = States.Pause;
		}
	}
	public static bool Won {
		get { 
			switch(state){
			case States.Won:
				return true;
			}
			return false;
		}
		set {
			if(value == true)
			state = States.Won;
		}
	}
}

The script is not instanced, and i want to keep it that way. You guys have any idea on how to make this script work globally by calling something like GameState.Pause ?

I just seems like alot of code for something simple.

using UnityEngine;
using System.Collections;
 
public class GameState {
    public enum States {
		Running, Died, Pause, Won
	}
    public static States state = States.Pause;

    public static void ChangeState(States stateTo) {
      if(state == stateTo) 
          return;  
      state = stateTo;  
    }
	
	public static bool IsState(States stateTo) {		
		if(state == stateTo)
			return true;
        return false;
	}

    public static bool IsRunning {
        get {
			return IsState(States.Running);
        }
    }
    
    public static bool IsPaused {
        get {
			return IsState(States.Pause);
        }
    }
	
	// You can still do this but will need GameState.Running = true;
	// ChangeState is more atomic...
    public static bool Running {
        get {
			return IsState(States.Pause);
        }
		set {
			if(value)
        		ChangeState(States.Running);
		}
    }
    // ...
}

Test it

	Debug.Log("state:" + GameState.state);
	Debug.Log("state:paused:" + GameState.IsPaused);
	
	GameState.ChangeState(GameState.States.Running);		
	Debug.Log("state:" + GameState.state);
	Debug.Log("state:running:" + GameState.IsRunning);
	
	GameState.ChangeState(GameState.States.Pause);
	Debug.Log("state:" + GameState.state);
	Debug.Log("state:paused:" + GameState.IsPaused);	
	
	GameState.Running = true;
	Debug.Log("state:" + GameState.state);
	Debug.Log("state:running:" + GameState.IsRunning);