Hi there folks,
currently making a little game for my uni project, I’ve stumbled upon a problem I failed to solve myself (including watching over a dozen videos on the subject). Currently, I’m having two scenes: the startmenu (index 0), and the game level(index 1). So the problem now is when I start the game within the editor, the game skips the start menu and jumps directly into the game. I tried to resolve the problem with a boolean value check, but it resulted in an infinite loop of both the scenes being loaded. I guess it’s just a little detail speck I may have missed, but could you guys help me settle this?
I’m providing you the code for the start menu manager and the game controller.
Start menu:
using UnityEngine;
using System.Collections;
public class StartMenuScript : MonoBehaviour {
public void endTheGame(){
Application.Quit ();
}
public void startTheGame(){
Application.LoadLevel (1);
}
}
Game controller:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject[] hazards;
public int hazardCount; //current count of hazards
public int hazardMax; //maximum amount of hazards at the same time during respawn
public int hazardStart; //start amount of hazards
public float boundaryX = 3f;
public float boundaryY = 5.5f;
public Text scoreText;
public Text restartText;
public Text gameOverText;
private bool withinCollider;
private bool restart;
private bool gameOver;
private int score;
void Start (){
restart = false;
gameOver = false;
scoreText.text = "Score: 0";
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
for (int i = 0; i < hazardStart; i++) {
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Instantiate (hazard,
new Vector3 (Random.Range (-boundaryX - 1f, boundaryX + 1f),
Random.Range (-boundaryY - 1f, boundaryY + 1f),
0),
Quaternion.identity
);
if (withinCollider) {
hazardStart--;
Debug.Log ("Asteroid innerhalb Safezone entfernt.");
}
}
hazardCount = hazardStart;
hazardMax = hazardCount;
}
void Update (){
if (restart) {
if (Input.GetKeyDown (KeyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
}
if(gameOver){
restart = true;
restartText.text = "Press 'R' to restart";
}
if (hazardCount < 6) {
for (int j = hazardCount; j < hazardMax; j++) {
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Instantiate (hazard,
new Vector3 (Random.Range (-boundaryX - 1f, boundaryX + 1f),
Random.Range (-boundaryY - 1f, boundaryY + 1f),
0),
Quaternion.identity
);
if (withinCollider) {
hazardStart--;
Debug.Log ("Asteroid innerhalb Safezone entfernt.");
}
hazardCount++;
}
hazardMax++;
}
}
public void AddScore (int newScoreValue){
score += newScoreValue;
UpdateScore ();
}
void UpdateScore (){
scoreText.text = "Score: " + score;
}
public void GameOver (){
gameOver = true;
gameOverText.text = "Game Over!";
}
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag == "Enemy"){
Destroy(col.gameObject);
withinCollider = true;
}
}
}
