Unity Community


Results 1 to 12 of 12

  1. Posts
    1,082

    GetComponent requires that the requested component...

    The warning :

    GetComponent requires that the requested component inherits from Component or MonoBehaviour.
    My code :

    Code:  
    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.
    UniTile - 2d Tile-based Map Editor for Unity - now also available in the Asset Store.

    My games :
    - ReRave (for iOS and Arcade)
    - Robin Hood - Archer of the Woods (published by Chillingo/Clickgamer)
    - Pig Shot (published by Nexx Studio)
    - Captain Ludwig
    - Pinkvasion
    - Deep
    - iPigeon


  2. Posts
    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.
    UniTile - 2d Tile-based Map Editor for Unity - now also available in the Asset Store.

    My games :
    - ReRave (for iOS and Arcade)
    - Robin Hood - Archer of the Woods (published by Chillingo/Clickgamer)
    - Pig Shot (published by Nexx Studio)
    - Captain Ludwig
    - Pinkvasion
    - Deep
    - iPigeon


  3. Location
    Zürich, Switzerland
    Posts
    26,572
    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. Posts
    1,082
    Thanks, but that seems like a lot of hastle/overhead to get rid of a warning, especially since it's completely functional...
    UniTile - 2d Tile-based Map Editor for Unity - now also available in the Asset Store.

    My games :
    - ReRave (for iOS and Arcade)
    - Robin Hood - Archer of the Woods (published by Chillingo/Clickgamer)
    - Pig Shot (published by Nexx Studio)
    - Captain Ludwig
    - Pinkvasion
    - Deep
    - iPigeon


  5. Location
    Zürich, Switzerland
    Posts
    26,572
    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. Posts
    1,082
    No, it works in all cases. But the warning is only shown for one instance...
    UniTile - 2d Tile-based Map Editor for Unity - now also available in the Asset Store.

    My games :
    - ReRave (for iOS and Arcade)
    - Robin Hood - Archer of the Woods (published by Chillingo/Clickgamer)
    - Pig Shot (published by Nexx Studio)
    - Captain Ludwig
    - Pinkvasion
    - Deep
    - iPigeon


  7. Location
    Zürich, Switzerland
    Posts
    26,572
    Ah I see
    thought it didn't work with the exception of one case


  8. Location
    Melbourne, Australia.
    Posts
    212
    I have the similar issue, but it is working some of the time and not for others.

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


    from this line.

    Code:  
    1. IActor victimController = (IActor)victim.GetComponent(typeof(IActor));

    The game object certainly does have a class which implements IActor.
    Last edited by Pip Robbins; 01-24-2012 at 04:42 PM.


  9. Posts
    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. Location
    Germany
    Posts
    527
    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. Posts
    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.

    Quote Originally Posted by marjan View Post
    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
    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 by Ace20; 03-11-2012 at 06:57 AM.


  12. Location
    London, UK
    Posts
    4
    Not sure if this is acceptable to you, but I ended up going with this:

    Code:  
    1.        Component[] components = collider.gameObject.GetComponents<Component>();
    2.  
    3.         foreach (Component obj in components)
    4.         {
    5.             if (obj is ITakeDamage)
    6.             {
    7.                 (obj as ITakeDamage).takeDamage(strength);
    8.                 health -= (obj as ITakeDamage).resistance;
    9.             }
    10.         }

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •