- Home /
gameObject.GetComponent("Script").enabled = true not working
Hi all,
I have created a script that controls Motion Blur image effect but "gameObject.GetComponent("Script").enabled = true;" not working, it says "BCE0019: 'enabled' is not a member of 'UnityEngine.Component'."
Thanks in advance for help ;)
Answer by Kryptos · Apr 03, 2012 at 01:41 PM
Edit -> Unity 5 : GetComponent("ScriptName") is deprecated (the string version). You must use one of the other versions.
Once again. NEVER use the string-based version of the method GetComponent which returns a Component (the compiler cannot infer the type). Instead, use the type-based method:
// JS
gameObject.GetComponent( Script ).enabled = true;
// C#
gameObject.GetComponent<Script>().enabled = true;
// or
(gameObject.GetComponent( typeof(Script) ) as Script).enabled = true;
What is the type of a C# script? The script name? If I don't use string of the script name in the <>, I always get an error "the type or namespace name 'ScriptName' can not be found, are you using a missing directive or an assembly reference?" What does this error message mean? thanks
Thanks, do you know why I get this error when I use the script type in the GetComponent
Because the script name doesn't exist or doesn't match the class name, I would assume.
It's because "enabled" is defined in Behaviour, not Component. It will work if you cast it to a Behaviour object, or any subclass of Behaviour.
Answer by Ninita · May 07, 2013 at 09:39 PM
or you could do (gameObject.GetComponent( "Script" ) as MonoBehaviour).enabled = true;
Answer by Hybris · Apr 03, 2012 at 01:00 PM
I think it has to be:
gameObject.GetComponent(Script).enabled
If that doesn't work try making a variable out of the GameObject the script is attached to. or try GetComponent("Script"//or just Script).active
You probably don't want that because it will disable the hole gameObject rather than just the component.
it did my whole game object, if i use the above scripts
gameObject.GetComponent( Script ).enabled = true;
where it says ( Script ) ,-- i enter the name of my script, it give me an error Uknown identifier
what should i do ?
when i tryed gameObject.GetComponent(script).enabled
it gives me error: cs0117.
Answer by frogsbo · Sep 02, 2014 at 12:54 PM
works:
camera.main.gameObject.GetComponent(MouseOrbit).enabled = true ;
Answer by Michio-Magic · Jun 13, 2015 at 07:09 PM
I had to use this > to access the First Person Controller component ...
gameObject.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController).enabled = false;
Your answer
Follow this Question
Related Questions
Trouble accessing scripts (Boo) 1 Answer
Deactivate Script 1 Answer
Gameobject script enable weird behavior 1 Answer