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 Mystfit · Oct 25, 2013 at 01:12 PM · getcomponentgenericsderived-class

Using GetComponent with base classes

I have a weird problem where I'm trying to access a component inside a gameobject by referencing its base class which is only returning a null. Here's how I'm trying to access my component.

 m_attachment = m_heldObject.GetComponent< BaseAttachment<BaseMusicObject> >();

In this situation, the actual component is of type InstrumentAttachment which extends BaseAttachment. The class is set up like this.

 public class BaseAttachment<T> : MonoBehaviour where T : BaseMusicObject 
 { 
     protected T m_musicRef;
     public virtual T musicRef{ get { return m_musicRef;}
     
     public virtual void Init(T managedReference){
         m_musicRef = managedReference;
     }
 }

InstrumentAttachment is initialized as a InstrumentAttachment object so that musicRef can be returned with the same type the generic class is initialized as. BaseInstrument extends BaseMusicObject. InstrumentAttachment is set up in this way.

 public class InstrumentAttachment : BaseAttachment<BaseInstrument>{
 }

Using GetComponent( typeOf(BaseAttachment < BaseMusicObject > ) ) as BaseAttachment < BaseMusicNote > ; will also fail, but accessing the component with its derived type using GetComponent() will work.

Am I doing something horribly wrong?

Comment
Add comment · Show 2
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 Loius · Oct 25, 2013 at 03:24 PM 0
Share

To show properly in text, you need to put spaces around them so the text formatter doesn't think they're html or whatever.

GetComponent( typeof( baseat ) ) as basat

If you're able to AddComponent your components with the proper types, you might need to cache the result of that function in another separate holder component to be able to consistently get the component :S.

avatar image Mystfit · Oct 26, 2013 at 06:56 AM 0
Share

Ah, thank you. This is my first question here so I'm not used to the formatting yet.

I definitely considered the caching idea, but the attachment class will either be created at runtime or added to a prefab, so I didn't have a consistent way of referencing it without using GetComponent. I've managed to work around the issue now regardless.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Azrapse · Oct 25, 2013 at 04:30 PM

I'm not 100% sure that you are being affected by this, but I suspect that you are making use of covariance and contravariance in generic types and those features were incorporated first into C# 4.0.

As far as I know, Unity uses C# 3.0.

Have a look at this: Covariance and Contravariance

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

Answer by Mystfit · Oct 26, 2013 at 06:36 AM

I managed to work around this problem by having a non-typed abstract class as the base class.

GetComponent< BaseAttachment>() now works since derived classes all have a common ancestor to work from.

Base Classes:

 public abstract class BaseAttachment : MonoBehaviour
 {
     public virtual void foo()
     {
     }
 }
 
 public class BaseAttachment<T> : BaseAttachment
 {
     protected T m_musicRef;
     public void Init(T managedReference){
         m_musicRef = managedReference;
     }
     public T musicRef{ get { return m_musicRef; }}
 }



Derived class:

 public class InstrumentAttachment : BaseAttachment<BaseInstrument>{
     public override void foo()
     {
     }
 }

This lets me run my overridden functions from any class derived from BaseAttachment, but offers me the ability to return the correctly typed musicRef object if working with the derived class.

Eg:

 GameObject instrument = new GameObject();
 instrument.AddComponent<InstrumentAttachment>().Init( new BaseInstrument() );

 //Runs overridden foo
 instrument.GetComponent<BaseAttachment>().foo();      

 //Runs overridden foo     
 instrument.GetComponent<InstrumentAttachment>().foo();     

 //Returns BaseInstrument
 instrument.GetComponent<InstrumentAttachment>().musicRef;  
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

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

How to GetComponent from Class that uses Generics 1 Answer

How do I reference a generic BaseClass using GetComponentInParent<>() 2 Answers

Why is this generic "Get component if null" code not working 5 Answers

Implicit type GetComponent call 0 Answers

Why use generics GetComponent() and what are generics 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