How do I use arrays, with getcomponent?

this is my script, im sure its simple, i just want all the objects with the tag Light to have their light component disabled once the player steps into the box collider. but nothing happens when i step into the collider.
i checked the tags are all right

var light2 : GameObject;

function Start () {
light2 = gameObject.FindGameObjectsWithTag (“Light”);


function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == “Player”){

for (var light : GameObject in light2){

light.GetComponent(Light).enabled = false;

please help, i have been at this for too long trying to find out how to fix it.

The method FindGameObjectsWithTag(string tag) is a static method.

You need to change your

light2 = gameObject.FindGameObjectsWithTag ("Light");


light2 = GameObject.FindGameObjectsWithTag ("Light");

so that you are using the GameObject class to call the method and not instance i.e. gameObject.

Edit #1:

Just tested out and seems like it is okay in Javascript to use an instance to call a static method. Unity returns the proper array of gameobjects.

It is only C# that will throw an error when trying to access static method with an instance.

  • Check if “OnTriggerEnter()” is being called by using “Debug.Log()” or debugger.
  • If yes then check if Player gameobject has been assigned the tag “Player”.
  • Finally check the length of light array. Is it filled correctly or not.