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.
Your question title says you "keep getting errors". Your question description says "nothing happens". These are vary different problems, and when asking for help you really need to be specific. What errors are you getting?
Sorry about that I was getting an error, but then I fixed the error so there was no error, I will fix the title
Answer by HarshadK · May 12, 2016 at 11:39 AM
The method FindGameObjectsWithTag(string tag)
is a static method.
You need to change your
light2 = gameObject.FindGameObjectsWithTag ("Light");
to:
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.
Good spot. Surely this should have thrown an error in the original code?
@tanoshimi 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.
So there is actually something else wrong with the code that is causing the issue for OP.
So my script works, for like transfor$$anonymous$$g the game objects, but I think the problem is the getcomponent part, did I do this right?
You are accessing the components correctly.
Just wrote this little code which works fine. It disables all light components in my scene when mouse button is clicked.
#pragma strict
var light2 : GameObject[];
function Start () {
light2 = gameObject.FindGameObjectsWithTag ("Light");
Debug.Log(light2.length);
}
function Update()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
for (var light : GameObject in light2){
light.GetComponent(Light).enabled = false;
}
}
}
So it is something related to your collision code itself. You need to check for issue there.
Deleted my previous comment because I was wrong, sorry. Checking for null is never a bad idea when you try to get a component from a gameobject.
Answer by UsmanAbbasi · May 12, 2016 at 11:35 AM
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.
Your answer
Follow this Question
Related Questions
Turn on/off renderer for objects found with "FindGameObjectsWithTag" 1 Answer
how to check when MoveTowards has finished 2 Answers
need help in randomising array 1 Answer
I cant figure out why my array is causing the error 'System.Type' does not support slicing. 0 Answers
how to not repeat random array 0 Answers