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 kayb14 · Jan 15, 2017 at 05:41 AM · c#coroutinescoroutine errorscalculationsstackoverflow

Stackoverflow by too many calculations?

Is it possible by to get the stack overflow exception dued to too many calculations in a single frame?

I have a function that calculates and compares the difference between 20 vector3 in a single frame. It submits the vector with the smallest distance,t to a coroutine and than restarts it.

the coroutine moves an object to a vector3 wich can be exchanged by the aforementioned function. note that the coroutine runs fine before the function is triggered!

when the function starts it's calculation I get a fps drop to "0" before getting the stackoverflow exception notice and the game runs on but doesn't execute the coroutine anymore. however the calculations are finished and the nearest vector3 is passed to the coroutine.

in a nutshell (coroutine=fine -> huge calculation -> change coroutine value -> restart coroutine -> stackoverflow)

I won't post any code right now, since it's rather just a question about the stackoverflow in general, since I couldn't find it's correct meaning. However if you really wanna help me debugging it, I will post the code.

Thanks a lot!

kayb14

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 steo · Jan 15, 2017 at 06:12 AM 0
Share

Do your calculations use some kind of recursion? You need to have a thousands method calls to get the stack overflaw. Have no idea how to get it with 20 Vector3 and without recursion.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Jan 15, 2017 at 08:20 AM

If I got this correctly you calculate distances between all vector3s, so 20x20 = 400 calculations. Vector3.Distance is quite expensive and it can possibly happen.

1) Try to calculate the smallest distance using (pos1 - pos2).sqrMagnitude instead of Vector3.Distance. This will avoid these expensive square root calculations. And in the end, once you find the smallest one, just return the square root of it.

2) Try to put such calculations to a separate thread.

3) Maybe you never do a proper yield.

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
avatar image
0

Answer by HenryStrattonFW · Jan 15, 2017 at 01:20 PM

It is more than likely an issue with your yield in the coroutine never being hit, thus the coroutine just starting over and over again without ever letting the frame end, or potentially an issue with your loop that is comparing the vectors never exiting. There is absolutely zero chance that comparing 20 Vector3 values even without using the sqrMagnitude optimisation in itself would cause you a stack overflow.

To be sure we would need to see the code you've written for the coroutine and calculations, then we can work out from that whats going wrong.

In the meantime I've written up this example. It creates a random colleciton of vectors and then starts a coroutine that will just find the nearest one to the transform once a frame. This will continue until StopComparisons is called, and can be restarted with StartComparrisons.

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour
 {
     [SerializeField]
     private float testScale = 10.0f;
 
     [SerializeField]
     private Vector3 nearest;
 
     private Vector3[] myVectors;
     private Coroutine comparisonRoutine;
 
     public void Awake()
     {
         myVectors = new Vector3[20];
         for (int i = 0; i < myVectors.Length; i++)
         {
             myVectors[i] = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * testScale;
         }
 
         StartComparisons();
     }
 
     public void StartComparisons()
     {
         // No sense starting it again if its already running.
         if (comparisonRoutine == null)
         {
             comparisonRoutine = StartCoroutine(_Compare());
         }
     }
 
     public void EndComparisons()
     {
         if (comparisonRoutine != null)
         {
             StopCoroutine(comparisonRoutine);
             comparisonRoutine = null;
         }
     }
 
 
     private IEnumerator _Compare()
     {
         while (true)
         {
             nearest = myVectors[0]; // since we always have a closest, lets start by assuming that it's the first item.
             float nearestDist = Vector3.Distance(nearest, transform.position);
 
             for (int i = 1; i < myVectors.Length; i++)
             {
                 float tempDist = Vector3.Distance(myVectors[i], transform.position);
                 if (tempDist < nearestDist)
                 {
                     nearestDist = tempDist;
                     nearest = myVectors[i];
                 }
             }
             yield return null;
         }
     }
 }
 

Notice that the while loop ensures the coroutine will continue running unless explicitly stopped (or if you added a break in for some reason, in which case you'd want to null the comparisonRoutine variable at the end of the coroutine).

Since you are calling this constantly you could just write a "FindNearest" method and call it in the update loop, however coroutines are useful as you could easily change it to only update the nearest every X frames or seconds.

Hope this example helps you work out where your initial code when wrong.

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

299 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Coroutines not running one after another 1 Answer

Need help using coroutines 1 Answer

c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers

Why isn't my coroutine working when I call it from another script. 0 Answers

Coroutine not executing second time 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