Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Best workaround to get transform of an class implementing an interface

Discussion in 'Scripting' started by Astrup, Oct 8, 2015.

  1. Astrup

    Astrup

    Joined:
    Jun 28, 2013
    Posts:
    5
    I have an interface ICard, which is implemented in several diffrent classes. I want to get the transform of these classes. I cannot call .gameobject or .transform as an interface cannot implement Monobehaviour. What would be your take on this?

    As of now, I instantiate these cards using prefabs and use .GetComponent<ICard>, if this is of any help.
    Thank you in advance :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you've instantiated the prefabs you already have access to their gameobject/transform from the instantiate function return... :confused:

    ... and if ICard doesn't inherit from monobehaviour, how are you calling getcomponent to access it? it's not a component
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This might work.

    Code (CSharp):
    1. public Interface ICard {
    2.     // ... Your stuff
    3.     Transform transform {get;}
    4. }
    I assume interfaces are allowed to pick up on properties or methods implemented on a base class that doesn't implement the interface.

    Let me know if it doesn't work and I will do some more digging.
     
    agourk, Raseru, Bunny83 and 1 other person like this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    GetComponent<SomeInterface> will return a component that has implemented SomeInterface.

    Since you know that GetComponent will only return a component, you can cast to a component instead.

    Code (CSharp):
    1. Transform cardTransform = ((Component)GetComponent<ICard>()).transform;
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    oh, didn't realise getcomponent did that... good to know :D
     
    Kiwasi likes this.
  6. stanogg

    stanogg

    Joined:
    Feb 22, 2015
    Posts:
    12
    Sorry for the necro, thought i could throw this in:

    Code (CSharp):
    1. public interface IMonoBehaviour{
    2. Transform transform { get; }
    3. GameObject gameObject { get; }
    4. T GetComponent<T>();
    5. .. other stuff
    6. }
    7.  
    8. public interface SomeInterface : IMonoBehaviour{
    9. ...
    10. }
    11.  
    12. You get the idea..
     
  7. eliweston

    eliweston

    Joined:
    Apr 6, 2018
    Posts:
    2
    I have a similar problem. This didn't work for me.
     
  8. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    We need more to work with than "didn't work" to help you. For example, did it cause an error? What error, specifically?
     
  9. spiritworld

    spiritworld

    Joined:
    Nov 26, 2014
    Posts:
    29
    Best workaround is to use inheritance instead of interface ;) Just today once again tried to utilize interface... and ended up using inheritance after ran into problems mentioned here: can't access transform or GetComponent without additional work.

    Thanks for the tip, didn't realize you can cast it into Component!
     
  10. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    291
    Use (interface as MonoBehaviour), e.g.

    Code (CSharp):
    1.                (usableItemsTotal[i] as MonoBehaviour).transform.SetParent(overlayMenu.transform, false);