I want to make a for statement that repeats constantly until a stop variable becomes "1". But wherever you type a "for" statement in any way or even without anything in the statement, Unity just crashes without even a report. So is there a way to achieve the same effect without using the for statement?

(Here's an example script that would crash.)

#pragma script
var stop : int;

fucntion Start()
  for(stop=0;stop<2;stop++ )

No matter where you put it, or what you do with it, or how you change it, it just crashes. So my question is: Is there an alternative to for statement?

  • \$\begingroup\$ Is that the only script you have attached to any GameObject in your scene? \$\endgroup\$
    – Savlon
    Aug 21 '15 at 15:13
  • \$\begingroup\$ @Salvon yes it is. \$\endgroup\$ Aug 21 '15 at 15:29
  • \$\begingroup\$ I agree with the accepted answer. I would start with a blank scene and keep adding the contents of your buggy scene until it starts freezing again... Obviously the one you just added is probably causing the issue. \$\endgroup\$
    – Savlon
    Aug 21 '15 at 15:34
  • \$\begingroup\$ The for statement works fine in Unity, I do it all the time. Something else is going wrong; to check, try deleting the object with this script to see if it works now. \$\endgroup\$
    – jhocking
    Aug 21 '15 at 15:45

The example you have posted should work fine. I would suggest there is something else wrong in your project if that is not working.

If your loop repeats too many times, or indefinitely as you have suggested in your question then you might cause Unity to hang, since it will wait for the loop to complete before it updates the next frame. You will need to use a coroutine and put your loop in there instead, if that is the case.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.