Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

unity manager.instance

Create your own custom GameManager script

//Example
public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    public static event Action<GameState> OnGameStateChanged;
    int UserScore = 0;
    int CompScore = 0;



    private void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void UpdateGameState(GameState newState) 
    {
        switch (newState)
        {
            case GameState.UserScored:
                HandleUserScored();
                break;
            case GameState.CompScored:
                HandleCompScored();
                break;
            default:
                throw new NotImplementedException($"No case block for state {newState}");
        }
        OnGameStateChanged.Invoke(newState);
    }
    
    private void HandleUserScored(){
    	UserScore++;
    }
    private void HandleCompScored(){
    	CompScore++;
    }
}

public enum GameState 
{
    UserScored,
    CompScored,
}
 
PREVIOUS NEXT
Tagged: #unity
ADD COMMENT
Topic
Name
9+6 =