GetComponent requires that the requested component...

Discussion in 'iOS Development' started by mudloop, Mar 9, 2010.

  1. mudloop

    mudloop

    Member

    Joined:
    May 3, 2009
    Messages:
    1,082
    The warning :

    My code :

    Code (csharp):
    1. this.anim = (IAnimationHandler)this.GetComponent(typeof(IAnimationHandler));
    The objects that implement this interface always extend MonoBehaviour.

    It all works, I just get this annoying warning. I completely get why it's there, but I wonder if there's a workaround so the warning doesn't appear.
     
  2. mudloop

    mudloop

    Member

    Joined:
    May 3, 2009
    Messages:
    1,082
    If anyone is wondering, I managed to get rid of the warning by storing the System.Type returned by typeof in a variable, and using that variable. No more mister warning.
    EDIT : or so it seemed. It solved it in one instance, but not in another. Now I'm confused.
     
  3. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,590
    the problem is that objects that inherit from monobehaviour and implement this are not enough. to search for the component the type returned there itself must be a descendant of monobehaviour

    what you will likely have to do is either:
    1) have a manager that "knows all instances of these implementations"
    2) make all your classes extend an own class that extends monobehaviour and implements this and then search for that one
     
  4. mudloop

    mudloop

    Member

    Joined:
    May 3, 2009
    Messages:
    1,082
    Thanks, but that seems like a lot of hastle/overhead to get rid of a warning, especially since it's completely functional...
     
  5. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,590
    GetComponent searches for objects that are of class Component or MonoBehaviour / MonoBehaviour extended.

    Your interface just does not meet this requirement.

    Its really strange that it seems to work in one case and I would suspect this one to be the bug, not vice versa.
     
  6. mudloop

    mudloop

    Member

    Joined:
    May 3, 2009
    Messages:
    1,082
    No, it works in all cases. But the warning is only shown for one instance...
     
  7. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,590
    Ah I see
    thought it didn't work with the exception of one case
     
  8. Pip Robbins

    Pip Robbins

    Member

    Joined:
    Oct 19, 2010
    Messages:
    213
    I have the similar issue, but it is working some of the time and not for others.

    Code (csharp):
    1. GetComponent requires that the requested component 'IActor' is inherited or implemented by 'CAGE'
    .


    from this line.

    Code (csharp):
    1. IActor victimController = (IActor)victim.GetComponent(typeof(IActor));
    The game object certainly does have a class which implements IActor.
     
    Last edited: Jan 25, 2012
  9. rhys_vdw

    rhys_vdw

    Member

    Joined:
    Mar 9, 2012
    Messages:
    2
    I have this same problem. I'm using GetComponentsInChildren() though. Strangely only a couple of the children cause this warning to be produced...
    Mudloop's fix made no difference. Does anybody else have this problem or a solution? I've used GetComponentInChildren with an interface type many times, but this is the first time I've received this warning.
     
  10. marjan

    marjan

    Member

    Joined:
    Jun 6, 2009
    Messages:
    538
    Tried this way?
    IActor victimController = Victim.GetComponent<IActor>();

    Same in JavaScript. Just there you have to write a dot after get component. I think. Well look up the docs for correct syntax
     
  11. Ace20

    Ace20

    New Member

    Joined:
    Oct 27, 2010
    Messages:
    20
    I'm having this issue as well. It's a warning that always happens whenever you pass an interface into GetComponent(Type type). Everything still works fine as you would expect, but it is pretty annoying since it clutters the console and (if I remember correctly) even has the possibility of causing release builds to lag whenever the program prints the warning to the console. I noticed this a while ago when I forgot to take a repeating Debug.Log() statement out of an Android release build, which caused the game to lag considerably.

    That way doesn't work, sadly. You get a compiler error that "The type 'IYourInterface' must be convertible to 'UnityEngine.Component' in order to use it as as a parameter 'T'".
     
    Last edited: Mar 11, 2012
  12. StubbornMinion

    StubbornMinion

    New Member

    Joined:
    Apr 13, 2012
    Messages:
    4
    Not sure if this is acceptable to you, but I ended up going with this:

    Code (csharp):
    1.  
    2.         Component[] components = collider.gameObject.GetComponents<Component>();
    3.  
    4.         foreach (Component obj in components)
    5.         {
    6.             if (obj is ITakeDamage)
    7.             {
    8.                 (obj as ITakeDamage).takeDamage(strength);
    9.                 health -= (obj as ITakeDamage).resistance;
    10.             }
    11.         }
    That is doing what I want, in that it's finding all components of type ITakeDamage. It would be better if GetComponent could do that itself, however.