Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

avoid using getcomponent in Update

Discussion in 'Scripting' started by xavicarosilvente, Nov 30, 2020.

  1. xavicarosilvente

    xavicarosilvente

    Joined:
    Nov 8, 2018
    Posts:
    8
    I would like to know if anyone has an idea on how to improve this ... As I understand it is a very bad idea to resort to getcomponent in update..

    upload_2020-11-30_18-21-40.png
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Usually you would get the reference once in Start and cache it. You still have to run null checks on it every time you access it since components could be destroyed.
     
  3. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    It's not really a "very bad idea"; at worst, it's "an area you might want to optimize later". It's a little bit slower than having a direct reference but in 99% of cases not actually all that slow; it's a dictionary lookup, which is one of the fastest "non-instant" things that can be done. Putting too much work into avoiding it is going to be a waste of time in most cases, taking attention and effort away from other optimization tasks (i.e. finding and fixing actual sources of slowdown via the Profiler.

    On the other hand, in situations where it's easy to remove them and has other benefits like code readability and reliability of references, then it's worth getting into the habit of using other ways to get the reference.

    If you DO want to reduce or remove these, we'd need to see some more context. What is "chatDisplayPrivate"? How is its value assigned? If I take a guess and say that it's probably of type List<GameObject> and its values are assigned in the inspector, then you should be able to make it type List<textChatPrivatePrefab> instead, assign it the same way, and then you can use that component directly. Plus, your textChatPrivatePrefab class can have its own reference to the TextMeshProUGUI that it contains (let's call it myText), so you could do something like:
    Code (csharp):
    1. public List<textChatPrivatePrefab> chatDisplayPrivate;
    2.  
    3. void Update() {
    4. for (int i = 0; i < chatDisplayPrivate.Count; i++)
    5. {
    6. if (chatDisplayPrivate[i].myText.enabled == true) {
    7. //etc
     
    Bunny83 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,446
    Ray_Sovranti essentially said it all. I just like to add if for some reason you can't or don't want to change your list to the actual component type you're interested in, there are still a few things you can do. First of all you have 3 GetComponent calls in one iteration to get the exact same component 3 times. Just get it once and store it in a local variable inside the loop. That has 3 main advantages: It obviously reduces the GetComponent call count. It also gives you an opportunity to give that variable a descriptive name which increases the readability of the code. It also shortens all the lines that had the GetComponent call in it which also improves the readability.

    Apart from this you have one GetComponent call that makes no sense in the first place because you just get the gameObject of that component. There is no point in doing GetComponent first since the object you call GetComponent on does already have a reference to the gameObject you're interested in.

    Finally: Please do not post code samples in images because:
    We can not quote or modify your code in our replies without retyping it from scratch.
    The code can not be found through a google search.
    This forum has code tags which can properly highlight code and also add scrollbars if the code is too long.