Search Unity

What is GetComponent<> ?

Discussion in 'Editor & General Support' started by Deleted User, Oct 24, 2019.

  1. Deleted User

    Deleted User

    Guest

    I have finished the Udemy course. I then checked the unity learning course. I still don't understand this. What does GetComponent<> do and what is the most basic item it controls? What is a beginner like me supposed to understand?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Not really sure what you mean with that.

    GetComponent dos give you a reference to some other component.
    You can specific what component you want to have access to by the the type in the pointed braces <> or as a string in the round braces.

    For example
    Code (CSharp):
    1. GetComponent<Light>().enabled = true
    or
    Code (CSharp):
    1. GetComponent("Light").enabled = true
     
  3. Deleted User

    Deleted User

    Guest

    That means is there anything get component CANNOT do?
    So that means based on the tutorials I have done, I can access all the items that an object has - as in circle collidor, rigidbody, transform position etc. Is that what you mean??
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It cannot alone get references to components attached to GameObjects which you don't already have a reference to.
    You're correct.

    One thing to be aware of is you should take care to not abuse it due to performance costs. GetComponent is pretty fast, but still takes time to complete. When possible you should cache its result, or get a reference to a component using faster means.

    For example, If script A needs to access a variable on script B several times per frame, the script would function correctly if every time it did so it used GetComponent. If you had a good number of these scripts in your scene all doing this, you'd eventually run into performance problems from GetComponent. This can easily be avoided by just calling GetComponent once on script A and saving the reference for later reuse.

    If in the example above both scripts were on the same prefab, it would be even better to just create a public field on script A and using the inspector you drag script B over to it. This then avoids all use of GetComponent.
     
    fffMalzbier and Vryken like this.
  5. Deleted User

    Deleted User

    Guest

    I need to revise through the projects I created to understand this better. A big thank you for your help.
     
  6. unity_72NNT-e0FQsyKw

    unity_72NNT-e0FQsyKw

    Joined:
    Feb 22, 2021
    Posts:
    13
    Joe gave a pretty good answer. I understand that I can call a function using getcomponant but then I was asking myself, why not just write a big script and make smaller ones call out all the time to get something I need? performance would be one problem.

    calling it to place into memory is cool too. I wonder what other ways are cleaner, even.
     
    Joe-Censored likes this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If possible, you can set references through the inspector instead of using GetComponent. That is often possible if you're making a prefab with several scripts all attached to the same GameObject, or between several GameObjects which are a part of the scene instead of being created at runtime. In that case you don't even need to use GetComponent.

    When you do need to call GetComponent, you can just call it once and cache the result so you don't need to do it again. Something like this:

    Code (csharp):
    1. private SomeOtherScript someOtherScriptInstance = null;
    2.  
    3. void Update()
    4. {
    5.     talkToSomeOtherScript();
    6. }
    7.  
    8. void talkToSomeOtherScript()
    9. {
    10.     if (someOtherScriptInstance == null)
    11.     {
    12.         someOtherScriptInstance = GetComponent<SomeOtherScript>();
    13.     }
    14.  
    15.     someOtherScriptInstance.DoSomethingCool();
    16.  
    17. }
    In the silly example above, I need to get a reference to SomeOtherScript and call a function on it every Update, but I only ever call GetComponent once to find it. A single call to GetComponent isn't a big deal. If you get into the habit of using it every time you need to talk to another script though, you may find you eventually are using GetComponent hundreds of times per frame. At that point the performance hit may start to add up.
     
    unity_72NNT-e0FQsyKw likes this.