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 2020using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; //Required for modifying any UI elementpublicclassMiniGameManager : MonoBehaviour{publicenumMiniGameState { idle, active, win, lose }publicMiniGameState curGameState;//TODO Add SpawnerpublicButton startButton; //Button component on the StartButton gameObjectpublicText resultsText; //Text component on the ResultsText gameObjectpublicCanvasGroup resultsPanelCG; //canvas group component on the ResultsPanelpublicint winScore =30;// Start is called before the first frame updatevoidStart() { curGameState =MiniGameState.idle; Utility.ShowCG(resultsPanelCG); //make sure panel is visibleresultsText.text="Score "+ winScore +" To Win";startButton.onClick.AddListener(ReStartGame); }// Update is called once per framevoidUpdate() {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 EVENTpublicvoidReStartGame() //important syntax {GameData.instanceRef.ResetGameData(); //use singleton variable to call public methodUtility.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>privatevoidGameOver() {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, 2020using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; //Required for modifying any UI elementpublicclassMiniGameManager : MonoBehaviour{publicenumMiniGameState { idle, active, win, lose }publicMiniGameState curGameState;publicSpawner spawner;//populate in inspectorpublicButton startButton; //Button component on the StartButton gameObjectpublicText resultsText; //Text component on the ResultsText gameObjectpublicCanvasGroup resultsPanelCG; //canvas group component on the ResultsPanelpublicint winScore =30;// Start is called before the first frame updatevoidStart() { curGameState =MiniGameState.idle; Utility.ShowCG(resultsPanelCG); //make sure panel is visibleresultsText.text="Score "+ winScore +" To Win";startButton.onClick.AddListener(ReStartGame); }// Update is called once per framevoidUpdate() {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 EVENTpublicvoidReStartGame() //important syntax {GameData.instanceRef.ResetGameData(); //use singleton variable to call public methodUtility.HideCG(resultsPanelCG); //toggle to hide panel curGameState =MiniGameState.active;//start spawnerspawner.activeSpawning=true;spawner.StartSpawning(); }/// <summary>/// Games the over./// Private since only executed from within this class/// </summary>privatevoidGameOver() {Utility.ShowCG(resultsPanelCG); //toggle to make visible///TODO stop spawner, destroy all spawned objectsspawner.activeSpawning=false;spawner.DestroyAllPickups();/// Get Text that is the child of the StartButton Text btnText =startButton.GetComponentInChildren<Text>();btnText.text="Play Again"; }}//end class