Search Unity

For loop 'argument is not enumerable' error [SOLVED]

Discussion in 'Scripting' started by QuinnWinters, Mar 7, 2015.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I'm attempting to enable or disable a multitude of box colliders on a child object. I assumed the following was the correct way to do this, but it doesn't ever hit the debug.log I stuck in there.
    Code (JavaScript):
    1. function OnTriggerExit (other : Collider) {
    2.  
    3.     if (other.tag == "Ranged") {
    4.         Debug.Log ("Exited Trigger Area");
    5.         for (var child : Transform in transform) {
    6.             if (!child.gameObject.activeInHierarchy) {
    7.                 continue;
    8.             }
    9.             for (var collider : BoxCollider in child) {
    10.                 Debug.Log ("Found Box Colliders");  //This never fires
    11.                 child.gameObject.collider.enabled = false;  //This never fires
    12.             }
    13.             child.gameObject.renderer.enabled = false;  //This works fine
    14.         }
    15.     }
    16.  
    17. }
    Due to it not hitting the debug.log I thought perhaps it wasn't seeing the colliders in the child because child is a transform and not a gameobject, but when I try it this way I get an "argument is not enumerable" error.
    Code (JavaScript):
    1. function OnTriggerExit (other : Collider) {
    2.  
    3.     if (other.tag == "Ranged") {
    4.         Debug.Log ("Exited Trigger Area");
    5.         for (var child : Transform in transform) {
    6.             if (!child.gameObject.activeInHierarchy) {
    7.                 continue;
    8.             }
    9.             var childObject : GameObject = child.gameObject;
    10.             for (var collider : BoxCollider in childObject) {  //Argument is not enumerable error
    11.                 Debug.Log ("Found Box Colliders");
    12.                 childObject.collider.enabled = false;
    13.             }
    14.             child.gameObject.renderer.enabled = false;
    15.         }
    16.     }
    17.  
    18. }
    What am I doing wrong here?
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Don't you need to do childObject.GetComponents<BoxCollider>() in JS?
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    That did the trick. That's something I didn't think to try for some reason. Thanks a lot!

    Corrected code:
    Code (JavaScript):
    1. function OnTriggerExit (other : Collider) {
    2.  
    3.     if (other.tag == "Ranged") {
    4.         Debug.Log ("Exited Trigger");
    5.         for (var child : Transform in transform) {
    6.             if (!child.gameObject.activeInHierarchy) {
    7.                 continue;
    8.             }
    9.             var childObject : GameObject = child.gameObject;
    10.             for (var collider : BoxCollider in childObject.GetComponents (BoxCollider)) {
    11.                 Debug.Log ("Found Box Colliders");
    12.                 child.gameObject.collider.enabled = false;
    13.             }
    14.             child.gameObject.renderer.enabled = false;
    15.         }
    16.     }
    17.  
    18. }