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 /
This question was closed Nov 26, 2014 at 09:54 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jort · Nov 26, 2014 at 07:44 PM · crashing

Unity crashes when running the following code

I'm trying to spawn a number of object flying over the screen from left to right. I'm using the follow code:

 public Vector2 o;
 public float timeLeft = 1f;
 public GameObject irri;
 
 void FixedUpdate () 
                     {
         o = irri.rigidbody2D.velocity;
         o.x = -2;
         irri.rigidbody2D.velocity = o;
 }

 IEnumerator Spawn2()
     {
         while (timeLeft > 0)
         {        
                 if (syanMode == 1)
             {
                 Vector3 spawnPosition2 = new Vector3 (2.0f, Random.Range (0, 0.5f), 0.0f);    
                 Instantiate (irri, spawnPosition2, Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f)));
                 yield return new WaitForSeconds ( Random.Range (0.1f, 0.5f));
             }
         }
 
 
     }



I have created an asset that has a rigidbody2d and a sprite, and assigned that to the variable irri in the scene.

as soon as I hit play to test if it works, unity freezes. why is that?

Comment
Add comment · Show 3
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 Jort · Nov 26, 2014 at 08:51 PM 0
Share

That helped! It's not spawning any objects now tho.. (I'm 100 % sure syanmode = 1)

avatar image meat5000 ♦ · Nov 26, 2014 at 08:53 PM 0
Share

Am I right in saying WaitForSeconds work in integers only?

Is Random.Range 0.1f possibly rounding down?

Does WaitForSeconds(0) work?

avatar image meat5000 ♦ · Nov 26, 2014 at 09:02 PM 0
Share

That helped! It's not spawning any objects now tho.. (I'm 100 % sure syanmode = 1)

Do you refer to @gjf comment?

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by gjf · Nov 26, 2014 at 08:13 PM

You need to yield if the 'if' isn't executed

Comment
Add comment · Show 2 · 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 Jort · Nov 26, 2014 at 09:35 PM 0
Share

yeah. had to put yield return in there. now it looks like this:

 IEnumerator Spawn2()
         {
             while (timeLeft >= 0)
             {        
                     if (syan$$anonymous$$ode == 1)
                 {
                     Vector3 spawnPosition2 = new Vector3 (2.0f, Random.Range (0, 0.5f), 0.0f);    
                     Instantiate (irri, spawnPosition2, Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f)));
                     yield return new WaitForSeconds ( Random.Range (0.1f, 0.5f));
                 }
                 yield return new WaitForSeconds (0.0f);
             }
     
     
         }


still no objects tho.

avatar image meat5000 ♦ · Nov 26, 2014 at 09:54 PM 0
Share

But it fixed the crash.

QA solved, followup quetions in a fresh QA please.

avatar image
0

Answer by johne5 · Nov 26, 2014 at 08:31 PM

I don't see where you're changing timeLeft to be less then 0, you might try timeLeft >= 0.

So the while loop is stuck in the loop

Comment
Add comment · Show 2 · 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 Bunny83 · Nov 26, 2014 at 08:45 PM 0
Share

It usually doesn't hurt being stuck in a while loop when executing a coroutine. However you have to invoke a yield return each iteration or you're really stuck inside the while loop doing nothing else.

avatar image Jort · Nov 26, 2014 at 08:48 PM 0
Share

Tried. still the same problem

Follow this Question

Answers Answers and Comments

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

Related Questions

Stop Android Crashing when doing a Callto: command? (or Switching from 2D to 3D?) 1 Answer

Game works fine in Unity but crashes after build 0 Answers

PC build crashing on launch everytime 1 Answer

unity 5 crashing when downloading from asset store 0 Answers

Access violation crashing my game on play 2 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