- Home /
How to use GetComponent?
How would I use GetComponent to change variables in other scripts? I understand the concept behind it, but I cannot setup the script in the right format. I used this (http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html) to set up my script. This is the line of code I am having trouble with:
transform.Find("box").GetComponent(lighttoggle).light = true;
In this part of the code, there is a box (gameobject), with a script attached called lighttoggle, With a var (bollean) called light.
But, when I attach the script to the player, the script does not turn on the light. The script works when I attach another trigger to it (Input.GetKeyDown), so the problem is the GetComponent. Can someone tell me how to format the GetComponent so that it will work?
Please don't ask me if I Googled it, I did. I will be grateful for any help. Javascript only, please
-Thanks
Answer by gheeler · Jul 03, 2013 at 02:45 PM
lighttoggle shoud be in "s
GameObject.Find("box").GetComponent("lighttoggle").light(.enabled) = true;
edit: yeah should be GameObject.Find
and you probably want .enabled = true
It is better to use GetComponent with a Type ins$$anonymous$$d of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name ins$$anonymous$$d of type.
Answer by dorpeleg · Jul 03, 2013 at 02:45 PM
I think Your first mistake is the fact that you are using transform.Find.
Either use GameOject.Find, or transform.Find(something).gameobject.
And what @gheeler said.
so it be GameObject.Find("box").GetComponent(lighttoggle).light = true;
Yea basically.
look at the link posted above, should help you understand better.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
How to use "GetComponent" and "transform" code in dll file? 0 Answers
disable/enable Mouse Look(c#) with a script of js 7 Answers
Object reference not set to an instance of an object 1 Answer