- Home /
Get Component Errors
In my scripts I'm trying to locate two other components and disable them from an inventory system.
My Script:
private var showGUI : boolean = false;
function Update()
{
if (Input.GetKeyDown ("i"))
{
showGUI = !showGUI;
}
if (showGUI == true)
{
Time.timeScale = 0;
GameObject.Find("Player").GetComponent(ClickToMove).enabled = false;
GameObject.Find("PlayerCam").GetComponent(UserCamera).enabled = false;
}
}
The script detects the Gameobjects: "Player", and "PlayerCam", but it does not detect the component scripts: "ClickToMove", and "UserCamera", even though I've checked, double checked, renamed, and triple checked, that the files were there, and that the names matched it doesn't detect either of the two scripts.
Any help is greatly appreciated.
Answer by Xarbrough · Jul 13, 2015 at 12:16 AM
GetComponent takes in the type of your script, which in C# you can do like this:
GetComponent(typeof(ClickToMove));
GetComponent<ClickToMove>( );
The second and easier version is called a generic function.
Also, you can avoid typing mistakes by using function overloads with constant values like this:
Input.GetKeyDown(KeyCode.A)
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Strange error with unity. 1 Answer
why won't my code work? 1 Answer
Cannot Open Javascript in MonoDevelop 1 Answer
Rigidbody2D just doesnt work :| 2 Answers