Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED] foreach statement cannot operate on variables of type 'BoxCollider'

Discussion in 'Scripting' started by zzmanzz, Apr 12, 2020.

  1. zzmanzz

    zzmanzz

    Joined:
    Jun 21, 2014
    Posts:
    27
    Hello,

    I am trying to create a code that selects multiple object units in my scene whenever I drag my mouse across them. However, I am getting this error:

    {
    "resource": "/c:/Users/Zach/RTSTest/Assets/Scripts/PlayerManager.cs",
    "owner": "csharp",
    "code": "CS1579",
    "severity": 8,
    "message": "foreach statement cannot operate on variables of type 'BoxCollider' because 'BoxCollider' does not contain a public instance definition for 'GetEnumerator' [Assembly-CSharp]",
    "source": "csharp",
    "startLineNumber": 63,
    "startColumn": 37,
    "endLineNumber": 63,
    "endColumn": 68
    }


    I am not sure what I've done wrong. I am very new to this. Here is the code itself, let me know if you need more to see.

    Also, if you know a way to maybe make this work more efficiently I am open to hearing those suggestions. I doubt selecting units this way is good.


    Code (CSharp):
    1. if(Input.GetMouseButtonUp(0))
    2.     {
    3.             if (isDragging)
    4.         {
    5.                 DeselectUnits();
    6.                 foreach(var selectableObject in FindObjectOfType<BoxCollider>())
    7.             {
    8.                 if(isWithinSelectionBounds(selectableObject.transform))
    9.                 {
    10.                     SelectUnit(selectableObject.transform, true);
    11.                 }
    12.             }
    13.         }
    14.          
    15.             isDragging = false;
    16.     }
     
  2. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    zzmanzz likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Change "FindObjectOfType<BoxCollider>()" to "FindObjectsOfType<BoxCollider>()".
     
    manoharansoundarraj and zzmanzz like this.
  4. zzmanzz

    zzmanzz

    Joined:
    Jun 21, 2014
    Posts:
    27
  5. zzmanzz

    zzmanzz

    Joined:
    Jun 21, 2014
    Posts:
    27
    smallest problems and I miss it lol yikes. Thank you.