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 LostInTime · Jul 19, 2013 at 07:08 PM · crashfunctionkinectwhile-loop

[Closed]Unity crash, while/for loop

Hi,

I am trying to create a script to use kinect to measure the user's arm lenght. As kinect's values fluctuate a lot when doing that, in my script I was making 150 measures and then calculating an average value - that would be admited as his real arm lenght throughout the rest of the code. (Just so you know, the calibration is supposed to start when you put your left hand in your hip - and you should have your right arm completely extended sideways)

The problem is that, as soon as I play the game, Unity crashes... Don't really get why...


 var calibrate : boolean = true;
 var calibrationValuesNumber : int = 150;
 var calculate : boolean = false;
 var valueSum : float = 0.0f;

 //lenght of the users arm during calibration
 var rightArmLenghtAprox : float;

 //lenght of the users arm after calibration
 var rightArmLenght : float;

 //distance between the user's left hand and his hip
 var leftDistanceToHip : float;

 function Start () {

   calibrationFunction();

 }

 function calibrationFunction() {

   while (calibrate == true) {
 
      leftDistanceToHip = Vector3.Distance(leftHand.transform.position, leftHip.transform.position);

      //This means that a user was detected and the skeleton has "expanded" and the distance between the body parts is no longer 0

      if (calculate == false && leftDistanceToHip > 0.23) {
     
         calculate = true;
         Debug.Log("calculating...");
     
      }         
     
      if (calculate == true && leftDistanceToHip <= 0.23) {
     
         Debug.Log("calibrating... stay still");
     
         for (var i : int = 1; i <= calibrationValuesNumber; i++) {
     
            rightArmLenghtAprox = Vector3.Distance(rightHand.transform.position, rightElbow.transform.position) + Vector3.Distance(rightElbow.transform.position, rightShoulder.transform.position);
            valueSum = valueSum + rightArmLenghtAprox;
             
            if (i == calibrationValuesNumber) {
                 
               rightArmLenght = valueSum / calibrationValuesNumber;
               calibrate = false;
               Debug.Log("Average is: " + rightArmLenght.ToString());
             
            }            
         }    
      }        
   }
 }


Thanks in advance

Comment
Add comment · Show 5
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 clunk47 · Jul 24, 2013 at 07:41 PM 0
Share

Please post the whole script. There are a ton of undeclared variables here, and I need to test in Unity.

avatar image roojerry · Jul 24, 2013 at 07:54 PM 0
Share

I would assume it is crashing because of it never exiting that while loop that is called on start. Why not run that function, $$anonymous$$us the while loop, in update until calibrate goes to false. That way the entire execution of your application isnt hung waiting for that while loop to finish.

avatar image robertbu · Jul 24, 2013 at 08:45 PM 0
Share

Click on the game object this script is attached to and verify that 'calibrationValuesNumber' is not zero.

avatar image highpockets · Jul 24, 2013 at 08:46 PM 0
Share

The update function is a while loop. I agree with brianruggieri. Try this:

function Update() {

 if( calibrate == true )
 {
 
 // do your stuff
 
 }
 
 else
 {
 
 // whatever other stuff
 
 }
 ..............
 
 }
avatar image LostInTime · Jul 27, 2013 at 01:35 PM 0
Share

The only problem in using the update function is that every frame it will be checking if that value is still the same or not... :/ it is a waste of processing power and I only need it to run once at the beggining

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ExplodingCookie · Jul 24, 2013 at 08:39 PM

I have had this issue a lot, my temporary solution is to use this line at the end of the while like this

 while(true) {
     ...
     yield WaitForSeconds(0.001);
 }

For some reason, Unity never crashes as long as there is a yield in the script

~ExplodingCookie

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 gregzo · Jul 27, 2013 at 01:41 PM 0
Share

This is a very bad solution, as in UnityScript it will de facto transform the function into a Coroutine which will eat up resources. If a while loop is infinite, debug it, don't yield it.

avatar image LostInTime · Jul 28, 2013 at 07:34 PM 0
Share

I actually think this would work, because it would give time for "calibrate" to get to "false" before starting another "while loop".

avatar image
0

Answer by jovino · Jul 24, 2013 at 08:47 PM

The condition in the for loop

 i <= calibrationValuesNumber;

don't let execute the last if:

 if (i == calibrationValuesNumber)

I think you want to do this:

  if (i == calibrationValuesNumber - 1)
Comment
Add comment · Show 5 · 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 jovino · Jul 24, 2013 at 08:49 PM 0
Share

Of course you can change also the loop:

 for (var i : int = 1; i == calibrationValuesNumber; i++) {
avatar image jovino · Jul 24, 2013 at 09:02 PM 0
Share

If you are going to call the calibrationFunction more times (not only in Start(), I have to agree too with agobrianruggieri, do it in Update() function.

avatar image LostInTime · Jul 27, 2013 at 01:44 PM 0
Share

I will call it more times, but just when the user chooses it in the menu :/ putting it in the update function would work, but it's kind of a waste

avatar image jovino · Jul 27, 2013 at 09:05 PM 0
Share

Yeah, it can be a waste then. You checked the conditions like I said? I think it crashes because it never get out of the loop...

avatar image LostInTime · Jul 28, 2013 at 07:14 PM 0
Share

The conditions where correct, but yes there is a problem with the "while loop"... I think it might be an overload of information (like I posted in the solution below) and that way it would crash because there was too much information to process (also meaning it would never get to te end of one of the loop).

avatar image
0

Answer by LostInTime · Jul 28, 2013 at 07:11 PM

First of all, thanks everyone for your feedback. I got it sorted out. I think that the problem was that while "calibrate" was "true", every frame the function would start the "while loop" and that would mean an immensity of measures because "calibrate" wasn't getting to "false" until another "while loop" had started - imagine 100 "while loops" running at the same time and each checking for their conditions (I am not sure though...).

So I took away the "while loop" and now it looks like this:

 function calibrateFunction() {

 leftDistanceToHip = Vector3.Distance(leftHand.transform.position, leftHip.transform.position);

     
     if (calculate == false && leftDistanceToHip > 0.23) {
     
         calculate = true;
         Debug.Log("calculating... Put your left hand in your hip and extend your right arm sideways");
     
     }
     
     if (calculate == true && leftDistanceToHip <= 0.23) {
     
         Debug.Log("calibrating... stay still");
     
         for (var i : int = 1; i <= calibrationValuesNumber; i++) {
     
             leftDistanceToHip = Vector3.Distance(leftHand.transform.position, leftHip.transform.position);
     
             rightArmLenghtAprox = Vector3.Distance(rightHand.transform.position, rightElbow.transform.position) + Vector3.Distance(rightElbow.transform.position, rightShoulder.transform.position);
             valueSum = valueSum + rightArmLenghtAprox;
             
             if (i == calibrationValuesNumber) {
                 
                 rightArmLenght = valueSum / calibrationValuesNumber;
                 calibrate = false;
                 calculate = false;
                 Debug.Log("Average is: " + rightArmLenght.ToString());
                 return;
             
             }
             
         }
     
     }
     
     if ((calculate == false && leftDistanceToHip <= 0.23) || (calculate == true && leftDistanceToHip > 0.23)) {
         
         yield WaitForSeconds(0.1);
         repeatFunction();
     
     }

 }

 function repeatFunction() {

   calibrateFunction();

 }

This way, with just "if"s, it will just repeat the function until both "calibrate" and "calculate" values become "false" and it works PERFECTLY :D

But still, isn't there a way, inside the function itself, to start it again without the need to create a second function ("repeatFunction")...?

Thanks again

Comment
Add comment · Show 1 · 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 LostInTime · Mar 09, 2014 at 01:47 AM 0
Share

Now I look back at this post and I realize the patience and understanding people had to have to answer to this post x) Thanks everyone

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

21 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

Related Questions

DoWhile Loop is crashing Unity. Why? 2 Answers

A node in a childnode? 1 Answer

Installing Unity 3D 4.3.4 on Windows 8.1 2 Answers

Crash app unity 4.5.5f1 on nexus 7 (Android 4.4.4) 0 Answers

Unity Crashing on Second function call 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