The warning :
My code :GetComponent requires that the requested component inherits from Component or MonoBehaviour.
- 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.
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.
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
Thanks, but that seems like a lot of hastle/overhead to get rid of a warning, especially since it's completely functional...
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.
No, it works in all cases. But the warning is only shown for one instance...
I have the similar issue, but it is working some of the time and not for others.
- GetComponent requires that the requested component 'IActor' is inherited or implemented by 'CAGE'
from this line.
- 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 05:42 PM.
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.
Tried this way?
IActor victimController = Victim.GetComponent<IActor>();
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.
Last edited by Ace20; 03-11-2012 at 07:57 AM.
Not sure if this is acceptable to you, but I ended up going with this:
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.