- Home /
GetComponentInChildren not working in a function, help please?
Alright, I've created a way to get a component from either the current object or from its children, if I try to do this in a script it works fine but when I try it in this static function it won't work.
here's the function:
public static function GetIPU(ob : GameObject):ItemPickUp{
var fppu : ItemPickUp = ob.GetComponent("ItemPickUp");
if(!fppu){fppu = ob.GetComponentInChildren(typeof(ItemPickUp));}
return fppu;
}
I've also tried this:
public static function GetIPU(ob : GameObject):ItemPickUp{
var fppu : ItemPickUp = ob.GetComponent("ItemPickUp");
if(!fppu){return ob.GetComponentInChildren(typeof(ItemPickUp));}
if(fppu){return fppu;}
}
Still no luck, please help me find out what is wrong with this.
I heard that GetComponentInChildren gets in the current object or its children but this is false from what I've tried.
I tried to edit the question but it didn't work, I forgot to mention that the GetComponent part works but not the GetComponentInChildren.
Answer by Wolfrik_Creations · May 27, 2016 at 01:57 AM
I got it to work!
This function will get components in children or in the current object even if they're inactive or a non-instantiated prefab.
public static function FindComponent(ob : GameObject, scriptType : Type):Component{
var temp = ob.GetComponent(scriptType);
if(!temp){
for(var i=0; i<ob.transform.childCount; i++){
var tempB = ob.transform.GetChild(i).GetComponent(scriptType);
if(tempB){temp = tempB;}
}
}
return temp;
}
so this is the fully finished statement:
if(favMenu){
lc=false;
ml.enabled=false;
mlH.enabled=false;
mlHC.enabled=false;
mlTP.enabled=false;
var radial : GUIStyle = defSkin.GetStyle("Radial");
var fz:ItemPickUp=Func.FindComponent(favs[0], ItemPickUp);
var fo:ItemPickUp=Func.FindComponent(favs[1], ItemPickUp);
var ft:ItemPickUp=Func.FindComponent(favs[2], ItemPickUp);
var fta:ItemPickUp=Func.FindComponent(favs[3], ItemPickUp);
if(GUI.Button(Rect(885-200, 400, 150, 150), fz.icon, radial)){
Network.Instantiate(Resources.Load("Spawns/"+favs[0].name), itemSpawnLocation.position, itemSpawnLocation.rotation, 0);
}
if(GUI.Button(Rect(885, 400-200, 150, 150), fo.icon, radial)){
Network.Instantiate(Resources.Load("Spawns/"+favs[1].name), itemSpawnLocation.position, itemSpawnLocation.rotation, 0);
}
if(GUI.Button(Rect(885+200, 400, 150, 150), ft.icon, radial)){
Network.Instantiate(Resources.Load("Spawns/"+favs[2].name), itemSpawnLocation.position, itemSpawnLocation.rotation, 0);
}
if(GUI.Button(Rect(885, 400+200, 150, 150), fta.icon, radial)){
Network.Instantiate(Resources.Load("Spawns/"+favs[3].name), itemSpawnLocation.position, itemSpawnLocation.rotation, 0);
}
}
Thank you, Bunny83 for helping me!
Answer by Bunny83 · May 26, 2016 at 11:42 PM
The "typeof" operator belongs to C#. In UnityScript you just need to use the typename.
// C#
GetComponent(typeof(ItemPickUp));
// UnityScript
GetComponent(ItemPickUp);
In C# the typeof operator returns the System.Type object for the given type. In UnityScript the type name itself does this.
var type : System.Type = ItemPickUp;
GetComponentInChildren does search on the given object as well as it's children on all levels. What have you tried which makes you believe that? Are you sure your component is enabled and the GameObject is active?
btw. Your second try won't work as not all code path return a value. A method with a return value always need to return something, even when it's null.
ps: You should never use a string in GetComponent or GetComponentInChildren. It's slow and error prone.
Oh, well I always use typeof and in that exact way but only in this function it didn't work.
I will try using no strings or typeof in a little bit when I get back on Unity.
I guess the reason why my questions were never answered is because I put them in the help room, thank you for replying!
Well, yes, it seems you can use "typeof" as well, but it's actually just wasted since it does work without as well. Furthermore using the type directly allows the compiler to infer the return type from the type parameter.
I just tested GetComponentInChildren and it does return the components attached to the given object, event in UnityScript ^^.
yeah but typeof is used like this: I can use someGameObject.GetComponent(typeof(ItemPickUp)).icon but I can't use someGameObject.GetComponent(ItemPickUp).icon
so that's what I use typeof for.
now after changing it to this:
public static function GetIPU(ob : GameObject):ItemPickUp{
var fppu : ItemPickUp = ob.GetComponent(ItemPickUp);
if(!fppu){fppu = ob.GetComponentInChildren(ItemPickUp);}
return;
}
it gives me a NullReferenceException on both now.
When I run GetComponentInChildren it only returns for the children, I will try it now.
Your answer
Follow this Question
Related Questions
Static Function Aliases? 4 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
"Failed to call static function because an object was provided" error? 1 Answer
Verification Exception with Custom JS Function 0 Answers
Fetching a script name for AddComponent from a text - class doesn't exist? 1 Answer