MiniGameManager

Code updated 9/17/2020

To complete Project 1: Create The following GameObjects:

MiniGameManager: Empty GameObject

ResultsPanel Page:

  • ResultsPanel: UI-Panel with CanvasGroup Component

  • ResultsText: UI-Text - child of ResultsPanel

StartButton Page:

  • StartButton: UI-Button

MiniGameManager.cs

This script will be on emptyGameObject: MiniGameManager You'll need to populate public fields in the inspector once you have created the other required gameObjects. See Images below:

Put Script on an empty gameObject in the Scene.

Overview - MiniGameManager.cs:

The MiniGameManager Class manages logic for:

  • Start Button to Start Gameplay

    • Function ReStartGame( ) is executed when the StartButton is clicked

      • Resets GameData

      • Hides the Results Panel:

      • Sets the curGameState to MiniGameState.active

      • Hide the StartButton so it's inactive (hidden)

      • Set the Spawner activeSpawning = true

      • Execute Spawner StartSpawning( ) method.

  • Polling - Code Executed in Update( ): - Checked Every Frame

    • Checks to determine what the current value of curGameState

    • if curGameState ==MiniGameState.active

      • Check that Health is still greater than 0

      • If Health is Greater than 0

        • Check Score, if Score > WinScore

          • Set MiniGameState.Win

          • Set win text for ResultsText.text

          • Call GameOver( )

      • Else if Health is Less than or equal to 0

        • Set MiniGameState.Lose

          • Set lose text for ResultsText.text

          • Call GameOver( )

  • GameOver( ) Method

    • Show ResultsPanel ( Use Utility to set CanvasGroup values)

    • Show StartButton - setActive is true, makes visible

    • Stop Spawner by activeSpawning = false

    • Spawner: StopAllSpawning( ) Also destroys all spawned objects

Code MiniGameState, MiniGameManager

See Version2 below for Included Spawner Code

//Updated Sept 17 2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;   //Required for modifying any UI element

public class MiniGameManager : MonoBehaviour
{

    enum MiniGameState { idle, active, win, lose }

    [SerializeField]
     MiniGameState curGameState;

    //TODO Add Spawner

    [SerializeField]
    Button startButton; //Button component on the StartButton gameObject

    [SerializeField]
    Text resultsText;   //Text component on the ResultsText gameObject

    [SerializeField]
    CanvasGroup resultsPanelCG; //canvas group component on the ResultsPanel

    [SerializeField]
    int winScore = 30;

    // Start is called before the first frame update
    void Start()
    {
        curGameState = MiniGameState.idle;
        resultsText.text = "Score " + winScore + " To Win";
        Utility.ShowCG(resultsPanelCG); //make sure panel is visible

        startButton.onClick.AddListener(ReStartGame);  //IMPORTANT - Method to be exectued when onClick event happens
    }

    // Update is called once per frame
    void Update()
    {
        if (curGameState == MiniGameState.active)
        {
            if (GameData.instanceRef.Health > 0)
            {
                if (GameData.instanceRef.Score >= winScore)
                {
                    //won the game
                    curGameState = MiniGameState.win;
                    resultsText.text = "You are a winner";
                    GameOver();
                }
            }
            else  //lost due to health
            {
                curGameState = MiniGameState.lose;
                resultsText.text = "You lost, so sorry, try again";
                GameOver();
            }

        } //end if
    } //end Update

    /// <summary>
    /// Restart the game
    /// </summary>
    /// Syntax for a method to be executed by a Unity Event
    /// MUST BE PUBLIC to be executed by Unity EVENT
    public void ReStartGame()  //important syntax
    {
        GameData.instanceRef.ResetGameData();  //use singleton variable to call public method
        Utility.HideCG(resultsPanelCG); //toggle to hide panel
        curGameState = MiniGameState.active;
        startButton.gameObject.SetActive(false);

        //TODO 
        //start spawner
    }

    /// <summary>
    /// Games the over.
    /// Private since only executed from within this class
    /// </summary>
    private void GameOver()
    {
        Utility.ShowCG(resultsPanelCG); //toggle to make visible
        ///TODO stop spawner, destroy all spawned objects
        /// Get Text that is the child of the StartButton
        startButton.gameObject.SetActive(true); //must set gameObject to active before finding child text
        Text btnText = startButton.GetComponentInChildren<Text>();
        btnText.text = "Play Again";
    }

}//end class

Version 2 - With Spawner Code Added

The code below is a simplified version, either version should work, select the one you prefer

//Sept 17, 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;   //Required for modifying any UI element

public class MiniGameManager : MonoBehaviour
{

    enum MiniGameState { idle, active, win, lose }

    [SerializeField]
     MiniGameState curGameState;

    [SerializeField]
    Spawner spawner;

    [SerializeField]
    Button startButton; //Button component on the StartButton gameObject

    [SerializeField]
    Text resultsText;   //Text component on the ResultsText gameObject

    [SerializeField]
    CanvasGroup resultsPanelCG; //canvas group component on the ResultsPanel

    [SerializeField]
    int winScore = 30;


    // Start is called before the first frame update
    void Start()
    {
        curGameState = MiniGameState.idle;
        Utility.ShowCG(resultsPanelCG); //make sure panel is visible
        resultsText.text = "Score " + winScore + " To Win";
        startButton.onClick.AddListener(ReStartGame);
    }

    // Update is called once per frame
    void Update()
    {
        if (curGameState == MiniGameState.active)
        {
            if (GameData.instanceRef.Health > 0)
            {
                if (GameData.instanceRef.Score >= winScore)
                {
                    //won the game
                    curGameState = MiniGameState.win;
                    resultsText.text = "You are a winner";
                    GameOver();
                }
            }
            else  //lost due to health
            {
                curGameState = MiniGameState.lose;
                resultsText.text = "You lost, so sorry, try again";
                GameOver();
            }

        } //end if
    } //end Update

    /// <summary>
    /// Restart the game
    /// </summary>
    /// Syntax for a method to be executed by a Unity Event
    /// MUST BE PUBLIC to be executed by Unity EVENT
    public void ReStartGame()  //important syntax
    {
        GameData.instanceRef.ResetGameData();  //use singleton variable to call public method
        Utility.HideCG(resultsPanelCG); //toggle to hide panel
        curGameState = MiniGameState.active;
        startButton.gameObject.SetActive( false );
        //start spawner
        spawner.activeSpawning = true;
        spawner.StartSpawning();
    }

    /// <summary>
    /// Games the over.
    /// Private since only executed from within this class
    /// </summary>
    private void GameOver()
    {
        Utility.ShowCG(resultsPanelCG); //toggle to make visible
        
        ///TODO stop spawner, destroy all spawned objects
        spawner.StopAllSpawning();
        
        startButton.gameObject.SetActive( true );
        /// Get Text that is the child of the StartButton 
        Text btnText = startButton.GetComponentInChildren<Text>();
        btnText.text = "Play Again";
    }

}//end class

Last updated