Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by VincentRodriguez · Apr 03, 2012 at 12:50 PM · gameobjectgetcomponentboolenabled

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 ;)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VincentRodriguez · Apr 03, 2012 at 01:05 PM 0
Share

I'm developing in iOS

5 Replies

· Add your reply
  • Sort: 
avatar image
15
Best Answer

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;
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yifan · Nov 26, 2012 at 07:04 PM 0
Share

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

avatar image Eric5h5 · Nov 26, 2012 at 07:53 PM 0
Share

Yes, the type is the script name.

avatar image Yifan · Nov 26, 2012 at 08:17 PM 0
Share

Thanks, do you know why I get this error when I use the script type in the GetComponent

avatar image Eric5h5 · Nov 26, 2012 at 09:36 PM 0
Share

Because the script name doesn't exist or doesn't match the class name, I would assume.

avatar image spectre1989 · Oct 02, 2013 at 04:57 PM 0
Share

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.

Show more comments
avatar image
7

Answer by Ninita · May 07, 2013 at 09:39 PM

or you could do (gameObject.GetComponent( "Script" ) as MonoBehaviour).enabled = true;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Patel-Sagar · May 26, 2014 at 10:38 AM 0
Share

This worked for me...awesome idea. thanks Ninita.

avatar image sammershotsflick · Mar 07, 2015 at 08:13 AM 0
Share

Hey, thanks! This worked for me too!

avatar image buttmatrix · Apr 14, 2016 at 08:49 PM 0
Share

Brilliant, thank you!

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image munaeem · Apr 03, 2013 at 01:19 AM 0
Share

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 ?

avatar image sholom1 · Oct 16, 2015 at 02:49 AM 0
Share

when i tryed gameObject.GetComponent(script).enabled it gives me error: cs0117.

avatar image Eric5h5 sholom1 · Oct 16, 2015 at 02:55 AM 0
Share

Read the accepted answer.

avatar image
0

Answer by frogsbo · Sep 02, 2014 at 12:54 PM

 works:

camera.main.gameObject.GetComponent(MouseOrbit).enabled = true ;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble accessing scripts (Boo) 1 Answer

Deactivate Script 1 Answer

Gameobject script enable weird behavior 1 Answer

Get bool value from one script to another (C#) 2 Answers

Enabling/Disabling Multiple Tagged objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges