Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 MonkeyZebraProductionsOfficial · Oct 08, 2020 at 09:08 AM · while-loop

This code is very expensive and is causing my Unity to crash, how do I prevent this?,This code is very resource intensive and causes my unity to freeze. How do I prevent this?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MathsManager : MonoBehaviour {

 private int[] index,awnser,result,multiplier;
 private int operaterValue,increment;
 private GameObject[] tempNumbers,tempSigns;
 private float time=0.0f; 
 
 public GameObject[] NumberArray, DragabeNumbers,PositiveNegative;
 //public GameObject[] DragabeNumbers;
 public Transform[] NumberSpawner, OperaterSpawner;
 public BoxCollider2D[] AwnserBoxes,DraggableNumbers;
 public float TimeLimit = 3.0f;
 // Start is called before the first frame update
 void Start()
 {
     index = new int[10];
     awnser= new int[4];
     result = new int[4];
     multiplier = new int[4];
     tempNumbers = new GameObject[8];
     tempSigns = new GameObject[4];
     increment = 0;
     for (int i = 0; i < index.Length; i++)
     {
         index[i] = Random.Range(0, 9);
     }
    
     
     for (int l = 0; l < OperaterSpawner.Length; l++)
     {
         operaterValue = Random.Range(0, 2);
         tempSigns[l]=Instantiate(PositiveNegative[operaterValue], OperaterSpawner[l].position, Quaternion.identity);
         //Debug.Log(operaterValue);
         if(operaterValue==0)
         {
             multiplier[l] = operaterValue +1;
         }
         else
         {
             multiplier[l] = operaterValue - 2;
         }
         
     }
     for (int k = 0; k < awnser.Length; k++)
     {
         awnser[k] = index[2 * k] + multiplier[k]*index[(2 * k) + 1];
         if (k > 0)
         {
             while (awnser[k] == awnser[k - 1])
             {
                 index[2 * k] = Random.Range(0, 9);
                 index[(2 * k) + 1] = Random.Range(0, 9);
                 awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
             }
         }
         while (awnser[k] <= 0)
         {
             index[2 * k] = Random.Range(0, 9);
             awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
         }
        
         while (awnser[k] >= 10)
         {
             index[2 * k] = Random.Range(0, 9);
             awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
         }
     }
     for (int j = 0; j < NumberSpawner.Length; j++)
     {
         tempNumbers[j]=Instantiate(NumberArray[index[j]], NumberSpawner[j].position, Quaternion.identity);
     }

     //Debug.Log(awnser[0]);
     //Debug.Log(awnser[1]);
     //Debug.Log(awnser[2]);
     //Debug.Log(awnser[3]);
 }

 // Update is called once per frame
 void Update()
 {
     for (int i = 0; i < AwnserBoxes.Length; i++)
     {
         for (int j = 0; j < DraggableNumbers.Length; j++)
         {
             if(AwnserBoxes[i].IsTouching(DraggableNumbers[j]))
             {
                 result[i] = j;
             }

         }
         if (awnser[i]==result[i])
         {
            Debug.Log("Yes");
         }
         else
         {
             Debug.Log("No");
         }
     }
     time += Time.deltaTime;
     
     if(time>=TimeLimit)
     {
         ShuffleNumbers();
         time = 0;
     }
 }
 
 void ShuffleNumbers()
 {
     int temp;
     temp = index[increment];
     index[increment] = Random.Range(0, 9);
     while(index[increment]==temp)
     {
         index[increment] = Random.Range(0, 9);
     }
     operaterValue = Random.Range(0, 2);
     Destroy(tempSigns[increment/2]);
     tempSigns[increment / 2] = null;
     tempSigns[increment / 2] = Instantiate(PositiveNegative[operaterValue], OperaterSpawner[increment / 2].position, Quaternion.identity);
     //Debug.Log(operaterValue);
     if (operaterValue == 0)
     {
         multiplier[increment / 2] = operaterValue + 1;
     }
     else
     {
         multiplier[increment / 2] = operaterValue - 2;
     }

     if (increment%2==0)
     {
         awnser[increment / 2] = index[increment] + multiplier[increment/2] * index[increment + 1];
     }
     else
     {
         awnser[increment / 2] = index[increment-1] + multiplier[increment / 2] * index[increment];
     }
     
     for(int k = 0; k < awnser.Length; k++)
     {
         while (awnser[k] <= 0)
         {
             index[increment] = Random.Range(0, 9);
             awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
         }

         while (awnser[k] >= 10)
         {
             index[increment] = Random.Range(0, 9);
             awnser[k] = index[2 * k] + multiplier[k] * index[(2 * k) + 1];
         }
     }
     Destroy(tempNumbers[increment]);
     tempNumbers[increment]=null;
     tempNumbers[increment]=Instantiate(NumberArray[index[increment]], NumberSpawner[increment].position, Quaternion.identity);
     //Debug.Log(awnser[increment/2]);
     
     if (increment>=NumberSpawner.Length-1)
     {
         increment = 0;
     }
     else
     {
         increment++;
     }
     
     
 }

}

Comment
Add comment · Show 1
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 ZWLSOFTWARE · Oct 08, 2020 at 10:18 AM 0
Share

check your while loops, they usually make unity crash for me when they keep running good luck

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · Oct 08, 2020 at 10:32 AM

If it is the Start() method that takes time and freezes, you can move it to Update() and set Time.timeScale = 0 to stop the game from advancing while you do your work 1 frame at a time. You must split your loops so they return after a period of time that is less than your frame-time, to maintain your frame-rate. When done you set a flag initDone=true and do the normal Update() code. But if it's the Update() that takes too long, you really need to reduce the amount of code you run each frame.

If your time-intensive work does not need to interact with the Unity-main-thread you can lift it out to another thread, that way you can keep the code as it is and avoid having to split the loops. It's no fun work to split the loops, will make your code look ugly too :/

 using System.Threading;
 
 Thread thread;
 ManualResetEvent evt = new ManualResetEvent(false);
 void LoadThread()
 {
     //your time intensive loop here
     //...
 
     //continue with next step in time intensive work...
     //evt.WaitOne();
     //...
 }
 
 ThreadStart ts = new ThreadStart(LoadThread);
 thread = new Thread(ts);
 thread.Priority = System.Threading.ThreadPriority.Lowest;
 thread.Start();
 
 //evt.Set(); //communicate with the thread, let it continue work
 
 if(!thread.IsAlive) { /*thread done*/ }

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

138 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

Related Questions

[Closed]Unity crash, while/for loop 3 Answers

Can I use a while() in the function OnDrawGizmos() ? 1 Answer

Rotate object as long as it's colliding 0 Answers

XmlTextReader problem 1 Answer

Coroutine "While" Setup 1 Answer


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