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 Cleger · Oct 24, 2012 at 07:26 AM · component

Component referenciated with a String?

Hello, I'm trying to make a script that activates a component in another object colliding, but I do not want the component is fixed, I will reference the component with a String to reuse this code to reference any component. I need to know if this can be done.

Here is the code that I want to do that I get error and below is the code that works but has the fixed component.

 //I have a script called Weapon
 
 var component:String; //(the string component="Weapon")
 
 function OnTriggerEnter (collider:Collider) {
     var compClass:System.Object;
     compClass=collider.gameObject.GetComponent(component);
     print(compClass.GetType()); // This return Weapon
     compClass.enabled=true;        // this give me an error
 }
 
 //----------------------------------------------------------
 
 //code that works but has the fixed component.
 
 function OnCollisionEnter (collision:Collision) {
     var compClass:Weapon;
     compClass=collision.gameObject.GetComponent(component);
     print(compClass.GetType());// This return Weapon
     compClass.enabled=true;
 }


Thanks

Comment
Add comment · Show 3
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 whydoidoit · Oct 24, 2012 at 07:29 AM 0
Share

$$anonymous$$ake compClass a string and set it to weapon and it will use the string version of GetComponent which will require a cast to be the correct type or System.Type should allow you to pass an actual type, which will return an object of that type should it exist. In both cases you will need some base class or interface if you want to work with the object - perhaps $$anonymous$$onoBehaviour is enough (for the enabled property?)

avatar image Cleger · Oct 24, 2012 at 09:25 AM 0
Share

Thank you very much $$anonymous$$ike, monobehaviour is the solution for this script. Excuse me I'm no expert, could you give me an example of how it would be used to compClass as string and use the string version getComponent?, And as would be used System.Type. I found the following code does not work.

 var weaponScript:System.Type= System.Reflection.Assembly.GetExecutingAssembly().GetType(component);
                             print(weaponScript.GetSituationState());//give me an error
avatar image fafase · Oct 24, 2012 at 10:22 AM 0
Share

You should be able to use a string for your component but it is a little slower. I remember getting some warnings when using them like "You are casting down" or something like this.

At least, the string use is explained in the docs as well.

What if you just let the compiler do the conversion and choose the appropriate type:

 var compClass=collider.gameObject.GetComponent(component);

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

How to Get TextMesh component from the Gameobject the script is attached to? 3 Answers

How to attach a prefab to a script that is added dynamically 0 Answers

Unable to enable script 0 Answers

Problem disabling a script 1 Answer

Turn on Script when collide with this object? 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