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 /
This question was closed Jun 04, 2019 at 01:46 PM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
1
Question by Retropaint · Jun 03, 2019 at 05:36 PM · mathfstackoverflow

StackOverflow error in Mathf.Sqrt function

I get a stackOverflow exception error for the Mathf.Sqrt function below:

 for(int i = 0; i < neighbourTiles.Length; i++) {
             if(neighbourTiles[i] != null) {
                 if(!hasCheckedDistance) {
                     distance = Mathf.Sqrt(Mathf.Pow(target.position.x - neighbourTiles[i].transform.position.x, 2) + Mathf.Pow(target.position.y - neighbourTiles[i].transform.position.y, 2));
                     Debug.Log(distance);
                     hasCheckedDistance = true;
                 }
             }
         }

I've checked around for the StackOverflow error and it seems to be an error whenever something happens infinitely, though I don't see anything that would cause it to do so and I even created the seperate hasCheckedDistance bool to make sure it's only checked once. Clearly I'm missing something obvious. I have set this function in awake but for some reason the error will only show up whenever I reactivate the object. There are times where the editor can even hang, so I have to force-close it via task manager. Very weird stuff.

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

2 Replies

  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 03, 2019 at 06:09 PM

It's very well possible that you get a StackOverflow exception when calling Mathf.Sqrt (or any method for that matter). Each and every method call requires some memory on the stack where the return address is stored so the method knows where it should return to once finished. This usually isn't any issue since once the method returns that memory is "released". However you most likely have some infinite recursion going on. So you have a method that calls itself directly or indirectly. That means you are still inside the method (so the stack frame that got allocated is still there) and you call the method again so another stack frame is allocated, etc... Usually you would get the StackOverflow when actually calling your problematic method. However it's possible that your recursion just pushed the stack right before its limit. Then you call Mathf.Sqrt and you run out of memory and the exception is thrown there.


Since you haven't shown any of the surrounding code we can't tell where you went wrong. The only thing that is certain (99.99%) that you have some infinite recursion going on. Since you seem to check "neighbors" you most likely trying to traverse some sort of grid or graph and didn't think about preventing checking the same tiles over and over again. If you have thought about that, your logic most likely has a flaw. Again since we don't see any of that, no further help can be provided.

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 Retropaint · Jun 04, 2019 at 12:35 PM 0
Share

Woops, I deleted the code that I used for the question because I wanted to start over. I'm still doing the exact same thing, and since I don't know the cause of the issue I'll most probably run into it again.

avatar image Bunny83 Retropaint · Jun 04, 2019 at 01:45 PM 0
Share

So the question is outdated / not reproducible so we can close it.

avatar image
-1

Answer by metalted · Jun 03, 2019 at 05:49 PM

Are you sure it is not happening in another piece of your code? The distance function is just pythagorian and could only result in a NaN when there is something wrong with the power function (if its even possible to use imaginary numbers, which i'm not sure). So the SQRT function is fine, both are powers so the numbers inside the SQRT will always be positive, this means NaN is out of the question. StackOverflow can happen when you for instance use a for loop on a list, and then add an item to the list in the loop. Maybe the length of your neighbourTiles array is changing while you are running the loop. I'm pretty confident we can fix the issue but i think we need a little bit more code, because this code looks fine to me.

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

Follow this Question

Answers Answers and Comments

107 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

Related Questions

Mathf.Approximatley if statement 0 Answers

Mathf clamp movement to Camera space in js 1 Answer

How to get a value between 0 and 1 2 Answers

A math issue. 3 Answers

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders. 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