- Home /
Space Invaders style game bug.
Hello,I'm making a space invaders style game. But there's a bug in my game and I can't find a way to fix it. I my game, I have the 'Asteroid' scene, where you shoot and collect points, and a 'shop' screen, where you can buy upgrades. My problem is when you press the 'Next Wave' button in the shop, my game starts the wave, but 2 times. Could some one please tell my what I'm doing wrong? This is my script for the 'Asteroid' scene:
var IsSpawning : boolean = false;
var CanLoadShop : boolean = true;
var CanLoadLoss : boolean = true;
var Asteroid : Transform;
var WaveNumber = 0;
static var Score : int = 0;
static var Lives : int = 3;
static var HighScore : int = 0;
static var EnemyCounter = 0;
var test : int = 3;
function Awake(){
DontDestroyOnLoad(transform.gameObject);
}
function OnLevelWasLoaded(level : int){
if(level == 3){ //The asteroid screen is 3
Debug.Log("Level 3 was loaded");
UpdateWave();
CanLoadShop = true;
CanLoadLoss = true;
}
if(level == 4){ //The shop screen is 4
CanLoadShop = false;
CanLoadLoss = true;
}
}
function Update(){
if(Lives <= 0 && CanLoadLoss){
if(Score > HighScore){
HighScore = Score;
}
Application.LoadLevel("ScreenLoss");
CanLoadLoss = false;
}
if(EnemyCounter == 0 && !IsSpawning && CanLoadShop){
Application.LoadLevel("ScreenShop");
}
}
function UpdateWave(){
WaveNumber++;
WaveFunction(WaveNumber);
Debug.Log("Wave: " + WaveNumber);
}
function WaveFunction(Wave : int){
IsSpawning = true;
var EnemysInThisRound = Wave * 4;
Debug.Log(EnemysInThisRound + "Enemys");
for(var i = 0 ; i < EnemysInThisRound; i++){
var position = Vector3(Random.Range(-6, 6), 12, 0);
Instantiate(Asteroid, position, Quaternion.identity);
yield WaitForSeconds(Random.Range(1, 1.5));
EnemyCounter++;
}
IsSpawning = false;
Debug.Log("Stopped");
}
Here's my shopscript:
function OnGUI(){
if(GUI.Button(Rect(267,0,77,33),"Next Wave")){
Application.LoadLevel("Asteroid");
Debug.Log("Pressed next wave");
}
}
If it could help, here's my debug log:
Thank you - Nelis
It seems like either your Level gest loaded 2 times or just the OnLevelwasLoaded function gets called 2 times. So i think the problem might not be in this part of your code. You should check the part where you call OnLevelwasLoaded.
Somewhere you are calling the OnLevelWasLoaded function when the level was loaded. This line probably gets executed 2 times.
Answer by ExTheSea · May 10, 2013 at 04:41 PM
So from what i can tell you have your AstroidManager to not be destroyed when a new scene gets loaded. Now if the Asteroids level gets reloaded the AstroidManager GO doesn't get destroyed but the one from the Scene exists too. This way when you load the scene there will be 2 AsteroidManager Gameobjects which both react to the OnLevelWasLoaded-Call. You should either don't have a new AM GO being in the scene when you load it or don't make it so that the AM GO doesn't get destroy when the new Level gets loaded.
Another thing i would try to make my game so that it works without reloading the Level the entire time. Just a tipp.
Your answer
Follow this Question
Related Questions
Tree colliders not working?!? 1 Answer
Can a script be loaded to a next scene? 3 Answers
I can't create a new scene??? 0 Answers
Why do i get 4 errors here? (Javascript) 1 Answer
Slenderman script problem 5 Answers