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 Asherian · Apr 14, 2018 at 04:57 PM · scripting problemloopinfinitefreezingnot responding

How to stop this infinite loop

I'm making a newtonian physics simulator as my computer science project but for some reason Unity keeps freezing as soon as I run the program and the whole application stops responding and the only way to stop it is through ending it with the task manager, however I suspect that the program has just entered an infinite loop as when I check the task manager it is still using up memory and the CPU. I really need this fixed as the deadline is in less than 2 weeks, any help on this would be greatly appreciated.

Here is the code I currently have for it, if anyone would like to use some of it for their own project then by all means use it.


UI:

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class UI : MonoBehaviour
 {
 
     public InputField massInput;
     public InputField ivInput;
     public InputField angleInput;
     public GameObject Ball;
     public GameObject Cube;
     public GameObject Car;
     public Text Error_Mass;
     public Text Error_IV;
     public Text Error_Angle;
     public Text Error_Dropdown;
     public static int mass;
     public static int initialVelocity;
     public static int angle;
     public Dropdown objects;
 
     //Use this for initialization
 
     void Start () {
         massCatch();
         ivCatch();
         angleCatch();
         objectCatch();
     }
 
     //This detects what object was chosen in the dropdown box and then destroys the game objects that were not chosen.
     public void objectCatch()
     {
         if (objects.value != 0)
         {
             Error_Dropdown.text = "";
         }
         if (objects.value == 1)
         {
             DestroyObject(Cube);
             DestroyObject(Car);          
         }
         else if (objects.value == 2)
         {
             DestroyObject(Ball);
             DestroyObject(Car);
         }
         else if (objects.value == 3)
         {
             DestroyObject(Ball);
             DestroyObject(Cube);
         }
     }
 
     public void angleCatch()
     {
         string angleInputString = angleInput.text;
         if (angleInputString == "")
         {
 
         }
         else
         {
             angle = Convert.ToInt32(angleInputString);
             if (angle > 0)
             {
                 Error_Angle.text = "";
             }
             else
             {
                 Error_Angle.text = "Please input suitable starting angle (0 - 90):";
             }
 
         }
     }
 
     //This takes the value inputted for the initial velocity and converts it into a variable to be used for calculations.
     public void ivCatch()
     {
         string velocityInputString = ivInput.text;
         if (velocityInputString == "")
         {
 
         }
         else
         {
             initialVelocity = Convert.ToInt32(velocityInputString);
             if (initialVelocity > 0)
             {
                 Error_IV.text = "";
             }
             else
             {
                 Error_IV.text = "Please input suitable initial velocity:";
             }
 
         }
     }
 
     //This takes the value inputted for the mass and converts it into a variable to be used for calculations.
     public void massCatch()
     {
         string massInputString = massInput.text;
         if (massInputString == "")
         {
 
         }
         else
         {
             mass = Convert.ToInt32(massInputString);
             if (mass > 0)
             {
                 Error_Mass.text = "";
             }
             else
             {
                 Error_Mass.text = "Please input suitable mass:";
             }
 
         }
     }
 
     //Update is called once per frame
     void Update()
     {
 
     }
 }



Forces:

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Forces : MonoBehaviour {
 
     public int objectMass = 0;
     public int initialVelocity = 0;
     public int theta = 0;                                                                           //initial angle of movement set by user in the UI
     private double hori_U;
     private double vert_U;
     private double g = 9.8;                                                                         //gravity
     private double t;
     private double vert_V; 
     private double hori_V = 0;
     private double hori_S;
     private double vert_V_S;
     private double depth_S; 
     public double horizontalAcceleration = 0;
     public double verticalAcceleration = 0;
     public static double horizontalForce = 0;
     public static double verticalForce = 0;
     public static double vert_Sm = 0;
     public GameObject Ball;
     public GameObject Cube;
     public GameObject Car;
     public Dropdown Dropdown;
     public Vector3 movementUpdate;
 
 
     // Use this for initialization
     void Start() {
         objectSettings();
         
         
     }
 
     //This sets the initial starting position that all of the game objects will go to when they are picked
     public void objectSettings()
     {
         if (Dropdown.value == 1)
         {
             Ball.transform.position = new Vector3(16, 27, 285);
         }
         else if (Dropdown.value == 2)
         {
             Cube.transform.position = new Vector3(16, 27, 285);
         }
         else if (Dropdown.value == 3)
         {
             Car.transform.position = new Vector3(16, 27, 285);
         }
 
     }
 
     // Update is called once per frame
     void Update()
     {
         objectMass = UI.mass;
         initialVelocity = UI.initialVelocity;
         theta = UI.angle;
 
         //Horizontal and Verticle component variables
         hori_U = initialVelocity * Math.Cos(theta);                                                    //horizontal initial velocity
         vert_U = initialVelocity * Math.Sin(theta);                                                    //vertical initial velocity                                                               
         t = (2 * initialVelocity * Math.Sin(theta)) / 9.8;                                             //time
         vert_V = -1;                                                                                   //vertical current velocity
         hori_V = 0;                                                                                    //horizontal current velocity
         hori_S = hori_U * t;                                                                           //horizontal displacement (X)
         vert_V_S = (vert_U * t) + (0.5 * g * t * t);                                                   //vertical displacement used to solve the current vertical velocity
         depth_S = hori_S;                                                                              //depth displacement      (Z)
         do
         {
             vert_V = Math.Sqrt((2 * g * vert_V_S) + (vert_U * vert_U));
         } while (vert_V < 0);
         vert_Sm = -(vert_U * vert_U) / (2 * g);
         do
         {
             vert_V = Math.Sqrt((2 * g * vert_V_S) + (vert_U * vert_U));
         } while (vert_V >= 0);
 
         double vert_S = ((vert_U + vert_V) / 2) * t;
 
         hori_V = Math.Sqrt((2 * g * hori_S) + (hori_U * hori_U));                                                             
 
         float x = Convert.ToSingle(hori_S);                                                            //x co-ordinate for translation
         float y = Convert.ToSingle(vert_S);                                                            //y co-ordinate for translation
         float z = Convert.ToSingle(depth_S);                                                           //z co-ordinate for translation
 
         movementUpdate = new Vector3(x, y, z);
 
         horizontalAcceleration = (hori_V - hori_U) / t;
         verticalAcceleration = (vert_V - vert_U) / t;
         horizontalForce = objectMass * horizontalAcceleration;
         verticalForce = objectMass * verticalAcceleration;
 
         if (Dropdown.value == 1)
         {
             Ball.transform.Translate(movementUpdate);
         }
         else if (Dropdown.value == 2)
         {
             Cube.transform.position = movementUpdate;
         }
         else if (Dropdown.value == 3)
         {
             Car.transform.position = movementUpdate;
         }
     }
 
 }
 



CalcBox:

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CalcBox : MonoBehaviour {
 
     public Text calculationBoxOutput;                   //calc box output
     public int mass = 0;
     public int initialVelocity = 0;
     public double hForce = 0;                                 //horizontal force of moving object
     public double vForce = 0;                                 //vertical force of moving object
     public double maximumVerticalDisplacement;
 
     // Use this for initialization
     void Start () {
         
     }
 
     // Update is called once per frame
     void Update () {
         mass = UI.mass;
         initialVelocity = UI.initialVelocity;
         hForce = Forces.horizontalForce;
         vForce = Forces.verticalForce;
         maximumVerticalDisplacement = Forces.vert_Sm;
         calculationBoxOutput.text = "Mass: " + mass + Environment.NewLine + "Initial Velocity: " + initialVelocity + Environment.NewLine + "Horizontal Force: " + hForce
             + Environment.NewLine + "Vertical Force: " + vForce + Environment.NewLine + "Peak of curve from start position: " + maximumVerticalDisplacement + " units"; 
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Asherian · Apr 14, 2018 at 10:29 PM

I've solved the problem it was my do while loop, it was causing an infinite loop of a very CPU taxing operation causing the application to crash. I've since traded the loops for two if statements containing the same code which now allows the code to run.

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

194 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 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 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 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 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 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 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

Input.GetMouseButtonDown(0) causing infinite loop in corotine? 0 Answers

Unity hangs with scripts on school device. 0 Answers

For loop not looping, Conditional break and return question? 0 Answers

Why does IndexOf not return the right index? 1 Answer

Unity freezes. I dont know why... 3 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