I've made a game with a Title Screen (Canvas), and a Start Button attached to the Title Screen. Points, GameOver, Restart, and even the Start Button sort of works, but it's possible to play the game before the user presses start.
Is there a way to make the GameObjects only appear after the button is clicked? I've tried to move and change the "public void StartGame()", but I can't get it right.
This is my first game ever, so hope the code provided is relevant.
Enemy.cs
void Start()
{
enemyRb = GetComponent<Rigidbody>();
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
player = GameObject.Find("Player");
}
GameManager.cs
public void StartGame()
{
powerup.transform.rotation);
score = 0;
UpdateScore(0);
isGameActive = true;
titleScreen.gameObject.SetActive(false);
}
void Update()
{
enemyCount = FindObjectsOfType<Enemy>().Length;
if (enemyCount == 0)
{
roundNumber++;
SpawnEnemyIntruders(roundNumber);
Instantiate(powerup, GenerateSpawnPosition(), powerup.transform.rotation);
}
}
void SpawnEnemyIntruders(int enemiesToSpawn)
{
for (int i = 0; i < enemiesToSpawn; i++)
{
int enemyIndex = Random.Range(0, enemies.Length);
Instantiate(enemies[enemyIndex], GenerateSpawnPosition(), enemies[enemyIndex].gameObject.transform.rotation);
}
}
public void GameOver()
{
restartButton.gameObject.SetActive(true);
gameOverText.gameObject.SetActive(true);
isGameActive = false;
}
PlayerController.cs
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerAudio = GetComponent<AudioSource>();
}
StartButton.cs
void Start()
{
button = GetComponent<Button>();
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
button.onClick.AddListener(StartThis);
}