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
1
Question by Deel · Aug 13, 2013 at 12:50 PM · c#controllergetcomponentstring

Can i give GetComponent a variabel instead of a scriptname?

Is it possible to give GetComponent a string? Because I have multiple vehicles and they all have their own controll script.

My idea:

         public GameObject tank;
         public string scriptName = "TankController";
         
         tank.GetComponent<scriptName>().enabled = true;
       
     
 
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

1 Reply

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

Answer by CHPedersen · Aug 13, 2013 at 12:52 PM

Yes, if you use the string version of GetComponent instead of the generic version:

     public GameObject tank;
     public string scriptName = "TankController";
     ((TankController)tank.GetComponent(scriptName)).enabled = true;

Or, if you just need the enabled property:

     public GameObject tank;
     public string scriptName = "TankController";
     ((MonoBehaviour)tank.GetComponent(scriptName)).enabled = true;


This should work just fine as long as you supply the name of a script that actually exists, of course. Beware that it's generally safer to use the generic version where possible, since a lot of potential errors can be caught at compile time instead of producing more obscure bugs which will be harder to catch later on. But if your scenario calls for it, it's perfectly fine to use the string version.

Comment
Add comment · Show 6 · 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 Deel · Aug 13, 2013 at 12:57 PM 0
Share

And how can i disable it? I don't get a match for disabling after the bracket

avatar image Joyrider · Aug 13, 2013 at 12:58 PM 0
Share

use tank.GetComponent(scriptName).enabled = false;

avatar image Deel · Aug 13, 2013 at 01:35 PM 0
Share

Then i get this errror: UnityEngine.Component' does not contain a definition for enabled' and no extension method enabled' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

avatar image CHPedersen · Aug 13, 2013 at 03:07 PM 0
Share

Yup, that's one of the disadvantages of using the string version. With the generic version, you get the actual type of the script through the generic type parameter, and it automatically has the enabled property because all scripts inherit from $$anonymous$$onoBehaviour, which is where it is defined.

But with the string version, you don't get your own script type or even $$anonymous$$onoBehavior, you just get the most base class it knows for all components, namely Component. So now, you have to static cast back to your own script to get to .enabled. I've updated my answer accordingly.

avatar image Deel · Aug 13, 2013 at 03:59 PM 0
Share

for one vehicle it works, but when i try to go in the second vehicle i get this error.

InvalidCastException: Cannot cast from source type to destination type. With this ((VehicleControll)car.GetComponent(scriptname)).enabled = true; i have the same problem than before.

For Info: The second Vehicle dont have the script VehicleControll, so it would only work with vehicles which has vehicleControll and not with planeControll(as example)?

i would like to to manage every vehicle with the same enter/exit script.

I think it would be the best, when i work with bools and check in front of what vehicle i stay?

Show more comments

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

GetComponent with variable script possible? 1 Answer

string.TrimEnd() not working as expected 1 Answer

How to split alphanumeric string into array of strings based from their type : alpha or numeric ? 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