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 sillyrabbit · Apr 20, 2012 at 10:25 AM · crashloopgridinfinite

Is this loop infinite?

I'm trying to write a loop to build a grid of tiles. Unity crashes when I start the game. No log errors, just breaks. Have to kill it. So I'm assuming this loop is infinite. I can't see why, but I think I could be missing something simple, but fundamental.

Any explanations of what I am doing wrong would be great. :) Thanks

 var wi : int = 2;
 var ex : int = 2;
 var cell : GameObject[4];
 var CellPrefab : GameObject;
 
 function Start () {
      StartCoroutine(BuildBoard());
 
     }
 
 function BuildBoard () {
 
     for(var i=0; i<ex; i++){
         for(var n=0; n<wi; n++){
             var y = 0;
             y = y++;
             cell[y] = GameObject.Instantiate(CellPrefab, Vector3(i, n, 0), Quaternion.identity);
             }
        }
     yield WaitForSeconds (200);
 }


 

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 gregzo · Apr 20, 2012 at 10:30 AM

1) var y = 0; y++; will always give you y=1! 2) are you sure you want to wait for 200 seconds?

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 sillyrabbit · Apr 20, 2012 at 10:35 AM 0
Share

Good point. Fixed the y counter. That doesn't seem to be the crash problem though.

I don't really want to wait 200 seconds, that was just in case it was continuously calling the function. Which it wasn't, of course.

avatar image Bunny83 · Apr 20, 2012 at 11:11 AM 0
Share

@gregzo: As i thought it will be 0 all the time and not 1 ;)

The problem is the order in which the operations are executed:

 y = y++;
 
  - assign a value to y
      - evaluate this value (y++)
          - remember old value
          - increase by one
          - return old value
      - assign the returned value to y

I would be "1" with a pre-increment:

 y = ++y;

But it would still make no sense to write it this way :D

Those pre and post increment / decrement operators can give you headache. Think of this:

 y = 1;
 x = y++ + y++;

x will be "3". First y++ returns 1 the second returns 2 because of the first increment but y itself is also 3 after this.

avatar image
0

Answer by Bunny83 · Apr 20, 2012 at 10:33 AM

I don't see any reason for this code to break. The only obvious thing is that you declared the "y" var inside the inner for-loop so for each iteration it's reset to 0.

Also y = y++; is a really strange expression and dependend on the implementation can give you undesired results. "y++" is already y = y + 1. It's the post increment operator which means the current value is returned and then it's incremented. Assigning the old value back to y it very strange.

Anyway, I'm pretty sure that doesn't crash your application. Maybe you have a faulty script on the CellPrefab?

Just to fic the loop:

 function BuildBoard ()
 {
     var y = 0;
     for(var i=0; i<ex; i++)
     {
         for(var n=0; n<wi; n++){       
             cell[y++] = GameObject.Instantiate(CellPrefab, Vector3(i, n, 0), Quaternion.identity);
         }
     }
     yield WaitForSeconds (200);
 }

edit

:D Just realised the 200 seconds ;) This won't crash the application but it might take a while.

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 sillyrabbit · Apr 20, 2012 at 10:46 AM 0
Share

What can i say, I'm strange. Also, fairly inexperienced at program$$anonymous$$g. The 200 seconds was just to see if it helped, which it didn't. Unity crashes regardless of timer. Crashes instantly. I've created a scene with just a blank mesh of a prefab, this script(with your fix), and nothing else. Still crashes.

I take it I must assume the problem lies in Unity?

avatar image Bunny83 · Apr 20, 2012 at 10:58 AM 0
Share

Ok, just found two more things, but they actually would prevent the script from compiling:

 var cell : GameObject[4];

  1. An array-type can't contain a size. The size have to be specified when the array is created

  2. You didn't create the array which would give you a "index out of bounds" exception at runtime.

Either create the array right here: var cell : GameObject[] = new GameObject[4]; // or var cell = new GameObject[4];

or initialize the array in your build function which is the better way since it can be calculate the size :

 var cell : GameObject[]; 
 
 // [...]
 
 function BuildBoard ()
 {
     cell = new GameObject[ex * wi];

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 crashes because of script 2 Answers

Infinite Looping Crash 2 Answers

A node in a childnode? 1 Answer

Unity crashes when using while 2 Answers

Manual Collision Detection through triggers... 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