• 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 Direisnecro · Nov 22, 2013 at 07:01 AM · javascriptcrashing

Unity Crashing. Javascript

This code has been the only code ive been changing the past few days, so it must be this script that is causing the crash on play. im guessing it has something to do with the while script? it was working fine untill i changed the position of the if statement if(currentTime >= 4) to inside the while loop (It was right before the while loop before)

 var Enemy : GameObject;
 var SpawnedUnits : int = 0;
 var CanRespawn : boolean = true;
 var currenttime : float;
 var TotalWaves : int = 5;
 var CurrentWave : int =  1;
 var UnitsInWave : int = 10;
 var SpawnTime : float = 1.5;
 function Update () 
 {
 
     currenttime += Time.deltaTime;
     
     if(CurrentWave <= TotalWaves)
     {
     
         
             while(CanRespawn == true && SpawnedUnits <= UnitsInWave)
             {
             
                 if(currenttime >= 4)
                 {
                     Spawn();
     
                     if(SpawnedUnits >= UnitsInWave)
                     {
                         CurrentWave = CurrentWave + 1;
                         UnitsInWave++;
                         SpawnTime -= 0.2;
                         SpawnedUnits = 0;
 
                     }
                 
                 }
             }
         
     }
     
 }
 
 function Spawn()
 {
     
     CanRespawn = false;
     yield WaitForSeconds(SpawnTime);
     Instantiate(Enemy,transform.position,transform.rotation);
     CanRespawn = true;
     SpawnedUnits = SpawnedUnits + 1;
     
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KiraSensei · Nov 22, 2013 at 10:06 AM

The only thing that I can find out is :

 SpawnTime -= 0.2;

This implies that SpawnTime can be negative after some time, and then

 yield WaitForSeconds(SpawnTime);

won't like it ...

Comment
Add comment · Show 3 · 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
avatar image Direisnecro · Nov 22, 2013 at 05:14 PM 0
Share

But if the game crashes when i push the play button, it doesnt even run long enough for SpawnTime to become negative

avatar image KiraSensei · Nov 22, 2013 at 06:02 PM 0
Share

So there should be something else that crashes the game. You should try deactivating one by one your game objects (with the check box on the top left corner in the inspector) to locate what script is wrong, and then edit your question and post the problematic script when you find it.

avatar image Direisnecro · Nov 22, 2013 at 06:25 PM 0
Share

if i move

 if(currenttime >= 4)

To right before the while statement:

 while(CanRespawn == true && SpawnedUnits <= UnitsInWave)

It works fine. so the problem is there, and i cant find out why. the working script looks like this

 if(currenttime >= 4)
 {
         
 while(CanRespawn == true && SpawnedUnits <= UnitsInWave)
 {


but thats the problem, i want there to be a pause inbetween each wave.

avatar image
0

Answer by Joniz · Nov 22, 2013 at 06:26 PM

The While loop never lets the CPU go long enough to get another Update(). You are infinitely looping.

To verify this, if you add an "else { return; }" to the currentTime check, it stops crashing. I am not saying this is the proper way to fix this though.

This is why it worked when the currentTime check is outside the while loop.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

My timer script makes Unity crash... 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

simple ball rolling mechanic 0 Answers

Loop Through Entire Array - Simon Game 1 Answer


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