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
1
Question by LegionIsTaken · Jan 03, 2012 at 05:48 PM · errorfor loop

for loop causes StackOverFlowException

Hi,

I create this for loop, but for some reason it gives the error "A StackOverFlowException has occurred". I really don't know anything about this error, nor do I know how to produce it.

  for (var hit : Collider in colliders){
             if(hit.rigidbody){
                 hit.rigidbody.AddExplosionForce(explosivePower, transform.position, explosiveRadius, 1.0);
                 }
                 
                 var dist = Vector3.Distance(transform.position, hit.transform.position);
                 var linearEffect : float = 1.0 - (dist/explosiveRadius);
                 
                 if(applyExplosiveDamage){
                      hit.gameObject.SendMessage("ApplyDamage",damage*linearEffect, SendMessageOptions.DontRequireReceiver);
                     //hit.gameObject.SendMessage("ApplyDamage",damage*linearEffect, SendMessageOptions.DontRequireReceiver);
                 }  
             }

I really hope there are some talented people out there who can help me.

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 Julien-Lynge · Jan 03, 2012 at 06:16 PM 0
Share

The context for this is very important - where is it being called? Please include the wrapping method (e.g. Update) and the other code in that method. We also need to know which line is throwing the error, which is returned with the error message (you should probably just copy the whole error message here). Since you don't have line numbers in the example code you posted, please also copy the specific line below.

1 Reply

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

Answer by luizgpa · Jan 03, 2012 at 06:28 PM

StackOverflowException Class

The exception that is thrown when the execution stack overflows because it contains too many nested method calls. (from MSDN)

This usually means that you have an unbounded recursion, that is, a function is calling itself over and over, until the call stack is full. (see more)

The piece of code you posted doesn't show, but I'm guessing that it's inside a function that is being called by ApplyDamage (or inside ApplyDamage itself) and colliders refers to the same objects on different instances of this script.

Eg.: there are 2 instances of this scripts in gameobjects A and B. In A, colliders has only the collider from B, and in B colliders there's the collider from A. If I'm guessing right, when ApplyDamage is called for A, it will call ApllyDamage from B, and than it will call ApplyDamage from A, and so on causing an Stack overflow.

One way to stop the recursion is using flags that prevent the same function to be called multiple times on an instace of your script.

Comment
Add comment · Show 3 · 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 LegionIsTaken · Jan 03, 2012 at 06:40 PM 1
Share

You were right. Thank you!

avatar image Bunny83 · Jan 03, 2012 at 07:02 PM 1
Share

Well explained ;)

+1

avatar image JohnD2012 · Jun 20, 2012 at 07:40 PM 1
Share

How could we survive without geniuses like you @luiz :P

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple for loop to change QualityLevel 1 Answer

2-Dimensional Array Error [CLOSED] 1 Answer

for loop error 0 Answers

Package directory not found for DataContractSerialization 1 Answer

error in firefox 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