- Home /
Enable/Disable Script on different GameObject
I have been reading similiar posts and the answers havent been working out for me so Im posting a question with my code and hopefully someone will see what Im doing wrong cause Im sure its something small. Its a enter a vehicle script. The script to fly the plane is on the "Jet" object in the hierarchy, it has a child gameObject, a trigger box to enter and start flying the plane. On that trigger box is where this code is.
ERROR: BCE0019: 'enabled' is not a member of 'UnityEngine.Component'
Here is the line that Im getting the error on and allows me to find the Jet named object in Heirarchy and turn on the Flight Controls script which is disabled.
GameObject.Find("Jet").GetComponent("Flight").enabled=true;
GameObject.Find("Jet").GetComponent("Flight").enabled=false;
The second line is on the other side of the else if statement when im getting out of the plane and need to turn off the plane controls and use the players controls again.
Hopefully Im just being a noob and you guys can see the problem.
Thanks
Answer by robertbu · Dec 11, 2013 at 04:19 PM
Your problem is that you are using quotes/string in your GetComponent() call. The result is that the compiler does not know the type at compile time, so it uses 'Component'. Component does not have 'enabled'. Change to:
GameObject.Find("Jet").GetComponent(Flight).enabled=true;
Thank you so much, that worked. I thought it had to be string cause it was a script or something, i dont know im new at this but thanks a lot for the quick response.
Answer by kamullis · Mar 21, 2016 at 09:37 PM
Hi, I'm trying to do the same thing, but it doesn't seem to be working. I am trying to access the FirstPersonController script on the FPSController so I can disable it for the pause menu. This is what I have:
GameObject.Find("FPSController").GetComponent(FirstPersonController).enabled = false;
This is giving me the following error:
The name 'FirstPersonController' does not exist in the current context.
Any suggestions for why this might not be working?
Thanks
Try this:
//For Javascript:
GameObject.Find("FPSController").GetComponent.<FirstPersonController>().enabled = false;
//For C#:
GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled = false;
Also consider storing your FPSController in a variable either in the Inspector or on Awake. GameObject.Find() is a pretty heavy operation, as is GetComponent.
It would look more like this:
//For Javascript
var fps : FPSController;
function Awake(){
//Set in inspector or in awake.
fps = GameObject.Find("FPSController");
}
//...
//...
fps.enabled = false;
//For C#
public FPSController fps;
void Awake(){
//Set in inspector or in awake.
fps = GameObject.Find("FPSController");
}
//...
//...
fps.enabled = false;
Thanks @seth_slax it actually appears that I am having issues accessing the FirstPersonController script from the standard asset FPSController. For some reason I can access other components I have added with no problem but the components on the standard assetsgive me the error:
The type or namespace name 'FirstPersonController' could not be found. Are you missing a using directive or an assemble reference.
Any idea why?
Thanks
Aaha! i actually found the answer in another post.
Dude, thanks - I went through at least 5 Unity forum pages before I saw your simple, correct answer.
Your answer

Follow this Question
Related Questions
enable/disable specific components 3 Answers
turn on a game object? 1 Answer
Enable and Disable Particle Transforming to Random Position when Disabled 0 Answers
GUItexture not showing on runtime or trigger 0 Answers
Disable Meshrenderer OnTriggerEnter 0 Answers