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. Dismiss Notice

Find all lights in a game object

Discussion in 'Scripting' started by VisualTech48, Jul 3, 2014.

  1. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Hi, I need a script to find all the lights within the gameobject, and change colors of the lights. So for I got this:
    Code (java):
    1.  
    2. for(var lig : Light in TheObject.gameObject.FindObjectsOfType(Light))
    3. {
    4.     lig.color = Color.Lerp(startcolor, endcolor, lerp);
    5. }
    6.  
     
  2. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Using GetComponentsInChildren will search the root game object and all of its children for all components of type.

    Code (csharp):
    1. for (var light : Light in TheObject.gameObject.GetComponentsInChildren<Light>())
    2. {
    3.   light.color = Color.Lerp(startcolor, endcolor, lerp);
    4. }
     
    VisualTech48 and ScarfCookies like this.
  3. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Thank you, works like a charm ;)