Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by Fredex8 · Mar 01, 2016 at 05:51 PM · c#errorgetcomponentmonobehaviourtype

Using GetComponent without knowing the script name?

I've seen this asked a few times without any real resolution and I am now thinking that this simply isn't supported. What I would like to do is use GetComponent to assign a variable in the selected object's script but since a number of different objects with different scripts could be selected 'SomeScriptName' is not going to be known.

 selectedObject.GetComponent<SomeScriptName>().someVariable = "Example";

I am aware that I can just use SendMessage instead and this may be a better solution all around but after some experimentation I found I can use this to get the name of the script:

 Type scriptNameTest = selectedObject.GetComponent<MonoBehaviour>().GetType();

Provided that I have using System; in there or use System.Type scriptNameTest That will return 'SomeScriptName' for whichever object is selected. The name of the script can be printed correctly so it seemed logical enough just to do this with that information:

 selectedObject.GetComponent<scriptNameTest>().someVariable = "Example";

However that will return an error of 'scriptNameTest' is a variable but is used like a type.

If I declare scriptNameTest globally rather than locally it will return an error of The type or namespace name 'scriptNameTest' could not be found (are you missing a using directive or an assembly reference?)

So I am assuming that it just isn't possible to use a variable as a class in a GetComponent but I am also quite new to C# so it is quite possible I've just missed something. It seems sort of like an unnecessary limitation to me though. I know there are lots of other ways I could achieve this but it would just be so simple to use this method if it actually worked...

Comment
Add comment · Show 4
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 hexagonius · Mar 01, 2016 at 10:51 PM 1
Share

The accepted answer from this post may give you a start in doing what you're trying to to:
http://stackoverflow.com/questions/2107845/generics-in-c-using-type-of-a-variable-as-parameter

I'm just curious why not creating a base class for all those classes that have that variable or implement an Interface handling that variable. both can be used as T for GetComponent() and it's easier to handle.

avatar image meat5000 ♦ · Apr 01, 2016 at 09:07 PM 0
Share

There was one time I encountered the need to dynamically link unknown scripts. In this case the common factor was that it was one single object looking out to many. I reversed the polarity, so to speak, and simply made those scripts point at the common object, injecting their references in to a List. $$anonymous$$y object could then access them all easily without ever having to have found the objects or their component names.

avatar image Fredex8 · Apr 02, 2016 at 03:36 AM 0
Share

I posted this prior to implementing any base classes so I can see now that even if this behaviour were possible it probably just wouldn't be very good practice. It is better for each enemy to just have a base Enemy class which can easily be accessed.

It still means then passing variables or functions from that base class to the other enemy script. If I could just get the other enemy script name and use that as the class it would probably be less efficient anyway though.

avatar image meat5000 ♦ Fredex8 · Apr 02, 2016 at 12:58 PM 0
Share

You are right about it not being good practice.

This is the trouble with the 'finding components' approach. Every case Ive seen has resulted in some inelegant clumpy mess. Its hard to get a truly dynamic approach.

If you know what is guaranteed to be on your object, you can find all components and rule out the ones you expect, leaving you with the names of the other attached scripts.

But these Generic Component methods are often subject to change and a few of them have even been deprecated. This leaves the whole thing feeling rather hacky, anyway.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Svarr · Apr 02, 2016 at 01:37 AM

As hexagonius said, you could implement an Interface containing a property all these objects should have in common, like an identifier (e.g. enum). You would search for the interface and get any script attached to that object which derives from that Interface.
Then you could just check the property for which script you have found.
Though I'm not sure whether calling methods that are not predefined by the Interface will work.

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

127 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 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 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 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 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 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

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

How to fix "no overload for method get component takes 1 arguments" 1 Answer

How do I get the type of a Monobehaviour? 2 Answers

typemask and type are null 1 Answer

Referencing a function in a C# script 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