Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nelis · May 10, 2013 at 02:09 PM · javascriptbugscenewavespace invaders

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: alt text

Thank you - Nelis

gfhxjxvbhnbvnbxvbnxvbnxv.png (18.6 kB)
Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ExTheSea · May 10, 2013 at 02:25 PM 1
Share

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.

avatar image Nelis · May 10, 2013 at 03:00 PM 0
Share

What do you mean with check the OnLevelWasLoaded function?

avatar image ExTheSea · May 10, 2013 at 03:16 PM 0
Share

Somewhere you are calling the OnLevelWasLoaded function when the level was loaded. This line probably gets executed 2 times.

avatar image Nelis · May 10, 2013 at 03:27 PM 0
Share

Do you know how to fix it, so it only gets called once?

avatar image ExTheSea · May 10, 2013 at 03:28 PM 0
Share

Well i kinda have to see the code where you call it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges