x


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.

more ▼

asked Jan 03 '12 at 05:48 PM

LegionIsTaken gravatar image

LegionIsTaken
452 24 30 34

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.

Jan 03 '12 at 06:16 PM Julien.Lynge
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 03 '12 at 06:28 PM

luizgpa gravatar image

luizgpa
851 4 9

You were right. Thank you!

Jan 03 '12 at 06:40 PM LegionIsTaken

Well explained ;)

+1

Jan 03 '12 at 07:02 PM Bunny83

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

Jun 20 '12 at 07:40 PM JohnD2012
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1952
x29
x5

asked: Jan 03 '12 at 05:48 PM

Seen: 2172 times

Last Updated: Jun 20 '12 at 07:40 PM