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 Brenden-Frank · Aug 21, 2012 at 08:53 PM · c#getcomponentmethodtypegeneric

Generic Component Declaration

I have a script I'm accessing via GetComponent as such:

 Script myscript = GetComponent<Script>();

But I also have variants such as:

 Script2 myscript = GetComponent<Script2>();

What I'm looking for is a generic way of declaring a method perameter so that I can have a function that receives both:

 void myfunc(Script scriptToAcess)
 {
      //Do things with the either script
 }

My problem is the way declaration works, my method can only accept Script or Script2. Is there a generic way to type these components so my method can accept either?

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

1 Reply

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

Answer by ScroodgeM · Aug 21, 2012 at 08:56 PM

create myfunc that takes Component type or MonoBehaviour type. this will let you pass any script to input

or, make all Script and Script2 and either as child of some abstract type, also create myfunc accepts this abstract - so you also be able to pass any chilf of abstract type to input

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 Brenden-Frank · Aug 21, 2012 at 09:31 PM 0
Share

I get this error when I use Component or $$anonymous$$onoBehaviour ins$$anonymous$$d of the actual script name.

Type `UnityEngine.$$anonymous$$onoBehaviour' does not contain a definition for `script$$anonymous$$ethod' and no extension method `script$$anonymous$$ethod' of type `UnityEngine.$$anonymous$$onoBehaviour' could be found (are you missing a using directive or an assembly reference?)

avatar image ScroodgeM · Aug 21, 2012 at 09:35 PM 0
Share

this caused by you trying to execute method on $$anonymous$$onoBehaviour without casting it to your type. do following:

if (scriptToAcess is Script2)
{
    Script2 s2 = scriptToAcess as Script2;
    s2.script$$anonymous$$ethod();
}
avatar image Brenden-Frank · Aug 21, 2012 at 09:42 PM 0
Share

But doesn't that defeat the purpose of a generic method that handles all of these different scripts if I'm having to manually cast each one?

I'd have to write another if statement with that code for Script3 and Script4 etc...

avatar image ScroodgeM · Aug 21, 2012 at 09:47 PM 0
Share

look to second solution in my answer then

avatar image Brenden-Frank · Aug 21, 2012 at 10:00 PM 0
Share

Second solution worked great. Created class Script.

classes Script1 and Script2 extend Script

$$anonymous$$ethod takes Script as an argument and allows Script1 and Script2 to be passed without errors. Thanks again!

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

get component type at runtime using generic GetComponent in C# 1 Answer

C# Generic return type 1 Answer

Multiple Cars not working 1 Answer

Referencing a c# component script from JS script 2 Answers

Distribute terrain in zones 3 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