This script will be on emptyGameObject: MiniGameManager You'll need to populate public fields in the inspector. See Images below:
Create The following GameObjects:
ResultsPanel: UI-Panel with CanvasGroup Component ResultsText: UI-Text - child of ResultsPanel StartButton: UI-Button MiniGameManager: Empty GameObject
Put Script on an empty gameObject in the Scene.
Overview - MiniGameManager.cs:
This 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 GameState to MiniGameState.active
Sets the StartButton so it's inactive (hidden)
Starts the Spawner
Set the Spawner's activeSpawning = true
Calls Spawner's StartSpawning( ) method.
Polling - Code Executed in Update: - Checked Every Frame
Checks to determine what the current MiniGameState is
if MiniGameState.active
Check that Health is still greater than 0
If Health is Greater than 0
Check Score, if Score > WinScore
Set MiniGameState.Win
Call GameOver( )
Else if Health is Less than 0
Set MiniGameState.Lose
Call GameOver( )
GameOver( ) Method
Set Result Text to Win or Lose message
Make ResultsPanel visible ( Use Utility to set CanvasGroup values)
StartButton - setActive is true, makes visible
Stop Spawner by activeSpawning = false
Destroy any remaining spawned objects
DisplayResult - This method is called when the Game
Code MiniGameState, MiniGameManager
See Version2 below for Included Spawner Code
//Feb 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Required for modifying any UI element
public class MiniGameManager : MonoBehaviour
{
public enum MiniGameState { idle, active, win, lose }
public MiniGameState curGameState;
//TODO Add Spawner
public Button startButton; //Button component on the StartButton gameObject
public Text resultsText; //Text component on the ResultsText gameObject
public CanvasGroup resultsPanelCG; //canvas group component on the ResultsPanel
public 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.TotalScore >= 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;
//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
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
//Feb 22, 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Required for modifying any UI element
public class MiniGameManager : MonoBehaviour
{
public enum MiniGameState { idle, active, win, lose }
public MiniGameState curGameState;
public Spawner spawner;//populate in inspector
public Button startButton; //Button component on the StartButton gameObject
public Text resultsText; //Text component on the ResultsText gameObject
public CanvasGroup resultsPanelCG; //canvas group component on the ResultsPanel
public 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.TotalScore >= 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;
//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.activeSpawning = false;
spawner.DestroyAllPickups();
/// Get Text that is the child of the StartButton
Text btnText = startButton.GetComponentInChildren<Text>();
btnText.text = "Play Again";
}
}//end class