I am completely new to Unity and have minimal experience with coding. I thought I could acquaint myself with Unity by following a tutorial by the Renaissance Coders on YouTube to recreate the game Flappy Bird.
This is the code for the “Tap Controller” that controls the bird. Starting on Line 59, there is code to decide what happens when the bird hits an object or scorezone. OnPlayerScored(); and OnPlayerDied(); events are supposed to be sent to the game manager but nothing happens. I have checked the code for errors but I can’t seem to find anything that is wrong! (Game Manager code is on line 56 through line 68). Any help would be much appreciated!
Tap Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class TapController : MonoBehaviour {
public delegate void PlayerDelegate();
public static event PlayerDelegate OnPlayerDied;
public static event PlayerDelegate OnPlayerScored;
public float tapForce = 10;
public float tiltSmooth = 5;
public Vector3 startPos;
new Rigidbody2D rigidBody;
Quaternion downRotation;
Quaternion forwardRotation;
GameManager game;
void Start(){
rigidBody = GetComponent<Rigidbody2D>();
downRotation = Quaternion.Euler(0, 0, -90);
forwardRotation = Quaternion.Euler(0, 0, 35);
game = GameManager.Instance;
rigidBody.simulated = false;
}
void OnEnable() {
GameManager.OnGameStarted += OnGameStarted;
GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
}
void OnDisable() {
GameManager.OnGameStarted -= OnGameStarted;
GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
}
void OnGameStarted(){
rigidBody.velocity = Vector3.zero;
rigidBody.simulated = true;
}
void OnGameOverConfirmed() {
transform.localPosition = startPos;
transform.rotation = Quaternion.identity;
}
void Update(){
if (game.GameOver) return;
if (Input.GetMouseButtonDown(0)){
transform.rotation = forwardRotation;
rigidBody.velocity = Vector2.zero;
rigidBody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
}
transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
}
public void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Scorezone")
{
//register a score event
OnPlayerScored(); //event sent to game manager;
//play sound
}
if (col.gameObject.tag == "Deadzone")
{
rigidBody.simulated = false;
//play a sound
//register a dead event
OnPlayerDied(); //event sent to game manager
}
}
}
Game Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public delegate void GameDelegate();
public static event GameDelegate OnGameStarted;
public static event GameDelegate OnGameOverConfirmed;
public static GameManager Instance;
public GameObject StartPage;
public GameObject GameOverPage;
public GameObject CountdownPage;
public Text ScoreText;
enum PageState {
None,
Start,
GameOver,
Countdown
}
int score = 0;
bool gameOver = true;
public bool GameOver { get { return gameOver; } }
public int Score { get { return score; } }
void Awake() {
Instance = this;
}
void OnEnable()
{
CountdownText.OnCountdownFinished += OnCountdownFinished;
TapController.OnPlayerDied += OnCountdownFinished;
TapController.OnPlayerScored += OnCountdownFinished;
}
void OnDisable() {
CountdownText.OnCountdownFinished -= OnCountdownFinished;
TapController.OnPlayerDied -= OnCountdownFinished;
TapController.OnPlayerScored -= OnCountdownFinished;
}
void OnCountdownFinished() {
SetPageState(PageState.None);
OnGameStarted();
score = 0;
gameOver = false;
}
void OnPlayerDied() {
gameOver = true;
int savedScore = PlayerPrefs.GetInt("HighScore");
if (score > savedScore) {
PlayerPrefs.SetInt("HighScore", score);
}
SetPageState(PageState.GameOver);
}
void OnPlayerScored() {
score++;
ScoreText.text = score.ToString();
}
void SetPageState(PageState state) {
switch (state) {
case PageState.None:
StartPage.SetActive(false);
GameOverPage.SetActive(false);
CountdownPage.SetActive(false);
break;
case PageState.Start:
StartPage.SetActive(true);
GameOverPage.SetActive(false);
CountdownPage.SetActive(false);
break;
case PageState.GameOver:
StartPage.SetActive(false);
GameOverPage.SetActive(true);
CountdownPage.SetActive(false);
break;
case PageState.Countdown:
StartPage.SetActive(false);
GameOverPage.SetActive(false);
CountdownPage.SetActive(true);
break;
}
}
public void ConfirmGameOver() {
//activated when restart button is hit
OnGameOverConfirmed(); //events
ScoreText.text = "0";
SetPageState(PageState.Start);
}
public void StartGame() {
//activated when play button is hit
SetPageState(PageState.Countdown);
}
}
Thanks in advance ;)!