Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by NinjaDesigns · Dec 11, 2013 at 04:17 PM · enable

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

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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;
Comment
Add comment · Show 1 · 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 NinjaDesigns · Dec 11, 2013 at 04:35 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 5 · 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 seth_slax · Mar 21, 2016 at 11:23 PM 0
Share

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;


avatar image kamullis seth_slax · Mar 25, 2016 at 01:33 PM 0
Share

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

avatar image kamullis kamullis · Mar 25, 2016 at 01:54 PM 0
Share

Aaha! i actually found the answer in another post.

link text

avatar image EclecticSteve seth_slax · Jun 05, 2018 at 08:18 PM 0
Share

Dude, thanks - I went through at least 5 Unity forum pages before I saw your simple, correct answer.

avatar image Nikhil11 · Aug 04, 2017 at 04:55 AM 0
Share

Thanks this help me lot

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

20 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 avatar image avatar image avatar image

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


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