• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wolfrik_Creations · May 26, 2016 at 11:13 PM · javascriptfunctionsstatic-function

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfrik_Creations · May 26, 2016 at 11:15 PM 0
Share

I tried to edit the question but it didn't work, I forgot to mention that the GetComponent part works but not the GetComponentInChildren.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 26, 2016 at 11:43 PM 0
Share

ps: You should never use a string in GetComponent or GetComponentInChildren. It's slow and error prone.

avatar image Wolfrik_Creations · May 27, 2016 at 12:09 AM 0
Share

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!

avatar image Bunny83 Wolfrik_Creations · May 27, 2016 at 12:19 AM 0
Share

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 ^^.

avatar image Wolfrik_Creations Bunny83 · May 27, 2016 at 01:03 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Static Function Aliases? 4 Answers

Verification Exception with Custom JS Function 0 Answers

Fetching a script name for AddComponent from a text - class doesn't exist? 1 Answer

assigned function declaration in unity javascript for different types 1 Answer

yield WaitForSeconds preventing further actions. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges