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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing child object light color

Discussion in 'Scripting' started by Thorles, Apr 5, 2015.

  1. Thorles

    Thorles

    Joined:
    Mar 31, 2015
    Posts:
    4
    Hi!

    I have an object with 5 child (4 Point light and a collider) and i want to change the color of the ligths when the collision happens. I need an invisible collider because the object looks like a donut, so technically i don't collide with it, instead of the camera goes through it.

    Code (JavaScript):
    1. mainObject=other.transform.parent.gameObject;
    2. for (var child : Transform in mainObject.transform) {
    3.         if(child.gameObject.name == "Point light")
    4.             child.Light.color = Color.green;
    5.     }
    But this does not work. I tried child.gameObject.color, child.color and so on but i could not find the solution.
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Hi @Thorles.

    Your "for" statement is working? You can get right transforms? And entering in this string comparison?
    If yes for above questions, you can try GetComponent instead of Light.

    Code (JavaScript):
    1. GetComponent.<Light>().color = Color.green;
    Try to find what is your real problem, otherwise it's a shoot in the dark.
     
    Thorles likes this.
  3. Thorles

    Thorles

    Joined:
    Mar 31, 2015
    Posts:
    4
    Sorry i should have been more precise, but thanks to you now it works like this:
    child.GetComponent.<Light>().color = Color.green;
    Thank you again!