Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Guppie1337 · Jul 28, 2015 at 11:05 AM · script.getcomponentdetectiontypetypecasting

Identify and Use Unknown Types

I have an object that moves about and must access the script of any object it collides with. Instead of going through and listing every script (Type), I was wondering if there was a way I could detect and use the type based off what I COULD receive.

 void OnCollisionEnter2D (Collision2D other)
     {
       GameObject enemy = other.gameObject;
       string enemyTagDetect = other.gameObject.tag;
         
       //All the script names end in "Controller". e.g. "GoblinController".
       Type EnemyScriptType = Type.GetType(enemyTagDetect + "Controller");
 
       //GoblinController(Type).
       EnemyScriptType enemyScript = enemy.GetComponent<EnemyScriptType>();
     
       enemyScript.Health -= damage;
     }

I may be understanding the use of Type.GetType or the Type declaration of "Type".

How do I use a string value as the "Type" of the expected type's name?

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 Polymo · Jul 28, 2015 at 11:18 AM 0
Share

I think it would be easier if all those objects inherited from an abstract base class with maybe an enum for their type, if even needed. After all you'll want to use a method /variable from that object, so you'll need to know atleast something about it. $$anonymous$$y suggestion is to use virtual or abstract methods/properties from the base class. Or as a different approach, maybe even a separate component for health in your example.

avatar image ScienceIsAwesome · Jul 28, 2015 at 11:53 AM 0
Share

ValooFX makes a good point, a lazier way to fix your script would be to use other.Send$$anonymous$$essage("ChangeHealth", -damage); and put a ChangeHealth(float change) function in every enemy script...

avatar image Guppie1337 · Jul 28, 2015 at 11:59 AM 0
Share

I was thinking about messing with abstract classes or Interfaces to access all of the information. It's not really the route I want to take, but I'll definitely try it if there is a better design approach.

1 Reply

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

Answer by LaneFox · Jul 28, 2015 at 11:33 AM

You need to use Reflection.

Rather than explain it, I would recommend looking at MSDN and the loads of tutorials online about Reflection so that you understand how it works, what you would use it for and why. It's magical pixie dust stuff but you have to understand it or you'll come back to it 5 minutes later and be able to read the code you just wrote.

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

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

Related Questions

How can I use a variable with getcomponent<>()? 2 Answers

how would I make an array of different scripts 1 Answer

Dynamic type, unknown in advance to reuse scripts 1 Answer

Unity GetComponent return value 2 Answers

.getComponent() creates an Instance? 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