Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by venom6942 · Dec 14, 2016 at 02:39 PM · crashtimeloopcrashing

Why this script crashes Unity

Hi, I've been working on a spawning script to end up spawning enemies/ asteroids with the intention of making them spawn faster (so after 10 seconds there's a spawn, then after 9 seconds another spawn and 8 etc.) but I haven't been able to get the code to work and now I can't find errors in the script but it's crashing Unity when ran. I think it could be something to do with the loop or time algorithms.

Any help on this would be greatly appreciated, thanks!

Also any of the comments in the code are mainly just for testing purposes. alt text alt text

screenhunter-06-dec-13-2250.jpg (61.4 kB)
2.jpg (48.3 kB)
Comment
Add comment · Show 4
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 Pengocat · Dec 14, 2016 at 02:51 PM 1
Share

You should post code using code tags ins$$anonymous$$d of posting an image.

avatar image Pengocat · Dec 14, 2016 at 03:00 PM 1
Share

the goto statement is what makes Unity crash since you are making an infinite loop. Like Landern mentions you should not use the goto statement.

avatar image Pengocat · Dec 14, 2016 at 05:21 PM 1
Share

$$anonymous$$aybe you can use this as inspiration to do what you want?

 using System.Collections;
 using UnityEngine;
 
 public class AsteroidSpawner : $$anonymous$$onoBehaviour
 {
     public GameObject largeAsteroid;
     public float initialSpawnTime = 10.0f;
     public float $$anonymous$$imumSpawnTime = 1.0f;
     public float spawnTimeDecrement = 1.0f;
     private float spawnTime;
     private Vector3 asteroidPosition;
 
     IEnumerator Start()
     {
         // Set initial spawntime
         spawnTime = initialSpawnTime + spawnTimeDecrement;
         // Wait 7 seconds before entering loop
         yield return new WaitForSeconds(7f);
         // Spawn the first Asteroid
         SpawnAsteroid();
         // Enter Infinite loop
         while (true)
         {
             var countdown = Time.time + spawnTime;
             while (countdown > Time.time)
             {
                 // Do nothing if Countdown is not finished
                 yield return null;
             }
             SpawnAsteroid();
             // Update countdown timer according to spawntime
             countdown = Time.time + spawnTime;
         }
     }
 
     void SpawnAsteroid()
     {
         float asteroidX;
         float asteroidY;
         if (RandomBool())
         {
             asteroidX = Random.Range(-11f, 11f);
             asteroidY = RandomBool() ? 5f : -5f;
         }
         else
         {
             asteroidY = Random.Range(-5.5f, 5.5f);
             asteroidX = RandomBool() ? 10f : -10f;
         }
         asteroidPosition = new Vector3(asteroidX, asteroidY, -1f);
         Instantiate(largeAsteroid, asteroidPosition, Quaternion.identity);
         DecreaseSpawnTime();
     }
 
     /// <summary>
     /// Decrease the spawntime by spawnTimeDecrement if more than $$anonymous$$imumSpawnTime
     /// </summary>
     void DecreaseSpawnTime()
     {
         // Do not decrease if spawnTime is less than $$anonymous$$imumSpawnTime;
         if (spawnTime < $$anonymous$$imumSpawnTime)
         {
             spawnTime = $$anonymous$$imumSpawnTime;
         }
         else if (spawnTime > $$anonymous$$imumSpawnTime)
         {
             spawnTime -= spawnTimeDecrement;
         }
     }
 
     /// <summary>
     /// Returns a random Bool
     /// </summary>
     /// <returns></returns>
     bool RandomBool()
     {
         var random = Random.value > 0.5f ? true : false;
         return random;
     }
 }
avatar image venom6942 Pengocat · Dec 14, 2016 at 08:07 PM 0
Share

Thank you so much, I used IEnumerator and it works perfectly!

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Landern · Dec 14, 2016 at 02:46 PM

Don't use goto statements. Use while loops with a breaking condition, do use InvokeRepeating and Coroutines to manage repeated events with timers, your goto statement probably has untended affects we can't account for in this small script, but the implementation will and is leading to poor results.

I do realize you have a constraint on execution using the timer greater than timerMax if statement, but this can be updated to use coroutines instead. No other issues seem to pop out from looking at your script. Though on line 93 you will probably want to adjust to use Quaternion.identity instead of a parameterless constructor which will indicate no rotation.

 Instantiate(LargeAsteroid, AsteroidPos, Quaternion.identity);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Crashing when time slows 1 Answer

Unity Editor constantly crashing 0 Answers

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x50 0 Answers

Application.TickGlobalCallbacks causing crashing! 0 Answers

Unity just started crashing. 0 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