Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameObject.GetComponent: Valid types?

Discussion in 'Scripting' started by Nanako, Sep 30, 2014.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Hi all. I'm looking at this documentation, it seems inadequate: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

    It takes a type of component as an input, however the page doesn't seem to contain a list of valid input types, or any link to one.

    I suppose i could estimate an appropriate type input from the Add Component panel within unity, however those component type names are user-facing and written with spaces in them. Simply cutting out the spaces seems like a leap of logic which doesn't necessarily make sense.

    Is there a list somewhere of all the code-correct component types?
     
    MagusBrashiva likes this.
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    oh and while i'm on the subject, what will this function return if the target object has multiple components of the target type? will it only return the first one in the list, or all of them as an array ?
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    anything that inherits from component.... there's a handful listed here under variables. MonoBehaviours (scripts) is another example.

    http://docs.unity3d.com/ScriptReference/Component.html

    It will return the first instance it finds I believe. You generally shouldnt have more than one type on each gameobject
     
    Nanako likes this.
  4. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    thank you for the link!
    Umm, why? And also how does this relate ? I can see, if it only returns the first, there might be a problem with using more than one of the SAME type, but why would i avoid using multiple components of different types? Kind of odd, since the tutorials are having me do exactly that, with some objects having scripts audiosources and lights on them.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Little misunderstanding here. He meant you should not have several scripts of the same type attached to the same object. Everything else is fine.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    no. I meant what I said, although I think you did slightly mis-understand. A gameobject might have a renderer, rigidbody, collider, scriptA, scriptB. But you wouldnt put more than one of each of these on a gameobject. keeping in mind, that a child gameobject could also have each of those components without causing problems.

    There are a handful of exceptions, but generally you wont have more than one of each type.

    ie. Multiple Rigidbodies = bad/dumb/wrong
     
  7. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    ah, i see!
    But....

    on the link mentioned above, i found a plural form of the function. unity seems to have singular and plural forms of quite s lot of functions o.o

    http://docs.unity3d.com/ScriptReference/Component.GetComponents.html

    So wouldn't this function make it perfectly okay to use multiples of the same type, too?
    That's something i'm definitely likelyt to do. For example, i'll probably make a music controller for a project, and attach all my music tracks to it, rather than having a seperate object for each ?
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    like I said, there are a handful of exceptions like hingejoints where you would have multiple instances on one gameobject.
     
    Nanako likes this.
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    A couple of notes to add here:
    A) GetComponent<SomeParentClass>() will return anything that inherits from that class, as well. GetComponent<Renderer>() will return MeshRenderers, SpriteRenderers, etc. This also applies to your own scripts, and helps make inheritance fun!
    B) There are many flavors of GetComponent available, including ones that return an array of objects of the type specified and ones that crawl through the hierarchy and find them in the children. GetComponentsInChildren<Renderer>(), for example, is really really useful if you want an object and all of its children to become invisible.
    C) In one of the exceptional cases @JamesLeeNZ is talking about, GetComponents<HingeJoint>() will get you an array of HingeJoints you can loop through.
     
    Z0leee87 and Nanako like this.
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    A music controller would normally be one component, with each track in an AudioClip array, so there wouldn't be any reason to use GetComponents.

    --Eric
     
    Nanako likes this.
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Not sure I ever really made that connection (despite doing it with colliders). Good point sir.
     
    Nanako likes this.
  12. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    well that sounds like a nice trick. i can definitely see myself using that :D

    thank you all for the advice, this thread has been helpful <3