x


Getting an error with my for/in loop!

I am trying to initiate a health drop when a particle collision takes place. I am trying to use a for/in loop to reference each possible collider and I am obviously doing something wrong. My code is:

function OnParticleCollision (other : GameObject)
{
 for (var zombie : GameObject in GameObject.Find("MainCamera").GetComponent("zombieArray"))
 {
  if (other == zombie)
  {
    ...other code...
  }

When run and a particle is shot I get this error on the line starting the for/in : ApplicationException: Argument is not enumerable (does not implement System.Collections.IEnumerable). Boo.Lang.Runtime.RuntimeServices.Error (System.String name) Boo.Lang.Runtime.RuntimeServices.GetEnumerable (System.Object enumerable) UnityScript.Lang.UnityRuntimeServices.GetEnumerator (System.Object obj) vomitHit.OnParticleCollision (UnityEngine.GameObject other) (at Assets/Scripts/vomitHit.js:29)

Any help is very much appreciated!

more ▼

asked Mar 20 '11 at 04:45 PM

bodomchild0926 gravatar image

bodomchild0926
9 7 7 11

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Your for-loop is trying to loop 1 component, not many. You need to use GetComponents instead of GetComponent:

function OnParticleCollision (other : GameObject)
{
 for (var zombie : GameObject in GameObject.Find("MainCamera").GetComponents("zombieArray"))
 {
  if (other == zombie)
  {
    ...other code...
  }
}

On a side note:

You should declare your Array of objects as its own variable before you loop them to increase performance and readability.

more ▼

answered Mar 20 '11 at 05:21 PM

Skjalg gravatar image

Skjalg
1.4k 24 33 50

Thank you for your help, I have no errors now but the collision won't register now for some reason..is there anything you may be able to think of that could help me out again?

Mar 20 '11 at 05:39 PM bodomchild0926
(comments are locked)
10|3000 characters needed characters left

For performance reasons you could also use Camera.main instead of GameObject.Find("MainCamera").

more ▼

answered Mar 20 '11 at 05:25 PM

efge gravatar image

efge
5.1k 5 14 40

thanks I will do that

Mar 20 '11 at 05:40 PM bodomchild0926
(comments are locked)
10|3000 characters needed characters left

I now have no errors now but the collision won't register now for some reason..is there anything anyone may be able to think of that could help me out again?

more ▼

answered Mar 20 '11 at 06:00 PM

bodomchild0926 gravatar image

bodomchild0926
9 7 7 11

I'd suggest you make a separate question for this, where you tell more in depth about your problems.

But to maybe this will help you:

Description OnParticleCollision is called when a particle hits a collider.

This can be used to apply damage to a game object when hit by particles. This message is sent to all scripts attached to the WorldParticleCollider and to the Collider that was hit. The message is only sent if you enable sendCollisionMessage in the inspector of the WorldParticleCollider.

Specifically the last part: "The message is only sent if you enable sendCollisionMessage"

Mar 21 '11 at 09:42 AM Skjalg
(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:

x304
x133
x97
x3

asked: Mar 20 '11 at 04:45 PM

Seen: 1494 times

Last Updated: Mar 20 '11 at 05:16 PM