Get Attched Light of Cube

Hi, ih have a cube object that is invisible, because its a trigger.
To this cube (trigger) I have attached a Spotlight, named “RedSpotlight”.

In my script TriggerCollider.js I have write down some lines of code.
My problem is how to get the attached light. The script is a component of the cube trigger.

#pragma strict

private var redlight : Light;

function Start () {
	redlight = GetComponent("RedSpotlight");
}

function Update () {

}

function OnTriggerEnter () 
{
	Debug.Log("TriggerEnter");
	redlight.enabled = true;
}

function OnTriggerStay ()
{
	Debug.Log("TriggerStay");
	redlight.enabled = true;
}

function OnTriggerExit () 
{
	Debug.Log("TriggerExit");
	redlight.enabled = false;
}

Instead of GetComponent(“RedSpotlight”); I have also tried gameObect.GetComponent(“RedSpotlight”); and this.GetComponent(“RedSpotlight”);

For GetComponent, you need to pass in the name of the actual component type. It looks at the list of components that are part of this game object.

I’m guessing what you actually mean is “I’ve given this object a child object called RedSpotlight, which has a light component attached”.

For that you need to first get the child, then it’s light:

//get the 'RedSpotlight' child, then get it's light component
redlight = transform.FindChild("RedSpotlight").GetComponent("Light");

Something like that - I’m a c# coder, but I think JS is pretty similar :slight_smile:

The GetComponent method does not take a GameObject’s name but rather it’s type.

Try using:

function Start () {
    redlight = transform.Find("RedSpotlight").GetComponent(Light);
}