Search Unity

How to use GetComponents() in C#

Discussion in 'Scripting' started by besuser, Mar 6, 2008.

Thread Status:
Not open for further replies.
  1. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    Took me a while to figure this out, so I thought I'd share it.

    Usually you pass a string to GetComponent (singular):
    Transform myTransform = GetComponent("Transform");

    But for some reason this doesn't work with GetComponents();

    If you try it without the quotes:
    NetworkView[] myArray = GetComponents(NetworkView);
    it's no good either.

    Finally I found a solution:
    NetworkView[] myArray = (NetworkView[])GetComponents(typeof(NetworkView));

    typeof() = magical goodness

    Hope this helps someone down the road =)
     
  2. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Yup, that's the way it's done ;-)

    The cool thing about using typeof(ClassName) is that you get a compiler error when you have a typo in ClassName. With GetComponent("ClasName") you might end up having a really bad time wondering why this doesn't work. With GetComponent(typeof(ClasName)) the compiler immediately tells you that there is no such type named "ClasName" and you can fix it immediately and painlessly ;-)

    Sunny regards,
    Jashan
     
  3. jamesflowerdew

    jamesflowerdew

    Joined:
    Apr 29, 2009
    Posts:
    10
    Also note :


    CharacterController pcontroller=gameObject.GetComponent("CharacterController") as CharacterController;


    this also looks very different in c# to it's far simpler javascript

    pcontroller = GetComponent(CharacterController);

    what a brain killer :?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    But JS hides what it does which will break your neck when you decide to move a script to the iphone which you won't be able to do if you use the "auto-magic" way ...
     
  5. febrarian2009

    febrarian2009

    Joined:
    Jul 27, 2009
    Posts:
    37
    but how is this done in Gameobject? Say if you have a code like this in javascript,
    Code (csharp):
    1.  
    2. var nextGO : GameObject = GetComponent(someScript).nextGO;
    how is it in C#?
     
  6. RenoRosco

    RenoRosco

    Joined:
    Dec 16, 2008
    Posts:
    67
    I think it have to be like that

    Code (csharp):
    1.  
    2. GameObject nextGO = ((someScript)GetComponent(typeof(someScript))).nextGO;
    3.  
    But i prefer to set an reference in the editor and just drag the scripts i need there on it so you don´t have to use GetComponent because it is slow if done often.
     
  7. s_guy

    s_guy

    Joined:
    Feb 27, 2013
    Posts:
    102
    Yeah, using string lookup is slower, and not necessary in usual cases. Also unless you find it "as" the class you want, you may get it morphed to a parent class. I think the precise way to do it this is:

    Code (csharp):
    1. Transform myTransform = GetComponent(typeof(Transform)) as Transform;
    That way, you get it by type and not name lookup, but also specifically get the exact derived class you want, and with Intellisense completion on that exact class's methods, even if it's a derived class. Incidentally, there is a convenient, built-in shortcut that does both of these in a more terse way:
    Code (csharp):
    1.  
    2. Transform myTransform = GetComponent<Transform>();
    You can see that the definition for this is what is in the previous example.

    Code (csharp):
    1. public T GetComponent<T> () where T : Component
    2. {
    3.     return this.GetComponent (typeof(T)) as T;
    4. }
     
    Last edited: Mar 11, 2013
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Please see the rules about pointless necro-posting. There was no need to reply to a post from 2008.

    --Eric
     
Thread Status:
Not open for further replies.