Search Unity

Any one have any idea why this dont work ?

Discussion in 'Scripting' started by forbidden, Nov 26, 2014.

  1. forbidden

    forbidden

    Joined:
    Mar 6, 2013
    Posts:
    36
    Never mind it works ....
    I just had a typo.. fixed code unless someone wants it..

    How to loop thru items under parent and assign their transforms by name.

    Code (CSharp):
    1.     Transform paintuse;
    2.     Transform paintfire;
    3.     Transform paintpick;
    4.  
    5.     // Use this for initialization
    6.     void Awake ()
    7.     {
    8.  
    9.         //get refrences to all child targets by their transform name.
    10.         foreach (Transform t in transform)
    11.         {
    12.             switch (t.name)
    13.             {
    14.             case "PaintTarget_Use":
    15.                 paintuse = t.GetComponent<Transform>();
    16.                 break;
    17.             case "PaintTarget_Pick":
    18.                 paintpick = t.GetComponent<Transform>();
    19.                 Debug.LogError("found");
    20.                 break;
    21.             case "PaintTarget_Fire":
    22.                 paintfire = t.GetComponent<Transform>();
    23.                 break;
    24.             }
    25.         }
    26.     }
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Maybe this is more correct? ;-)
    Code (csharp):
    1. foreach (Transform t in GetComponents<Transform>()) {
    2.    ...
    3. }
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Oddly enough, Transform implements IEnumerable so that is perfectly legal; albeit weird looking.