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.

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.

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!