- Home /
Question by
marius.treu · Jan 28, 2013 at 05:24 PM ·
typegeneric
Generic function does not work...
Hi I'm trying to implement an easy generic function
I want to find out if a component (like a script, collider, etc.) is attached to an transform node.
Actually GetComponent should return null if that Kind of Type is not attached. What am I doing wrong?
Here some code snippets:
public static bool rootParentHasComponent(Transform node) { //... if(node.GetComponent()!=null) return true; //... }
Comment
Best Answer
Answer by Berenger · Jan 28, 2013 at 05:28 PM
T isn't declared here. Try this one :
public static bool rootParentHasComponent<T>(Transform node) {
//...
if(node.GetComponent<T>()!=null) return true;
//...
}
But that will generate an error, because T need to be a Component, so :
public static bool rootParentHasComponent<T>(Transform node) where T : Component
{
//...
if (node.GetComponent<T>() != null) return true;
//...
}
thx that was it, but it WAS declared... this Forum just does not Show it.... on your code aswell :P \ think it has to be escaped
Damn, you're right ! Well, it was the where thing then ^^