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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Ways to reference an object ? Which way is fastest/most efficient ?

Discussion in 'Scripting' started by marplast, Jun 24, 2020.

  1. marplast

    marplast

    Joined:
    Jun 21, 2020
    Posts:
    15
    Hello my friends,

    I decided to rewrite my code to avoid using FindObjectOfType as I thought it might be easier on the PC if I just link the other object in the inspector given that it is only one and exists at the beginning of the scene. Basically I have two objects Timer and Text object displaying the time. This is the code I came up with but I find that I had to reference the serialized text object but then I needed a second variable to hold the text component. Can you write this in a more efficient way please as I find I do a lot of this type of referencing at the moment ?

    Code (CSharp):
    1. public class Timer : MonoBehaviour
    2. {
    3.     [SerializeField] Text timerTextObject;
    4.     Text timerText = default;
    5.     float timeForLevel;
    6.     public static bool IsTimerElapsed;
    7.  
    8.     private void Start()
    9.     {
    10.         IsTimerElapsed = false;
    11.         timerText = timerTextObject.GetComponent<Text>();
    12.         timeForLevel = levelManager.TimeForLevel;
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         timeForLevel -= Time.deltaTime;
    18.         timerText.text = Mathf.Round(timeForLevel).ToString();
    19.         if (timeForLevel <= 0)
    20.         {
    21.             timeForLevel = 0;
    22.             IsTimerElapsed = true;
    23.             levelManager.TimeElapsed();
    24.         }
    25.     }
    26. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I'm confused as to the purpose of timerTextObject and timerText. They are both variables of type Text and you're simply setting one equal to another. And why are you doing GetComponent on Text object?

    You can either make timerTextObject public or leave it as is (SerializeField makes a private variable visible in the inspector) and it will show up in the inspector. Drag and drop the gameObject with the Text component into the variable slot and just replace timerText.text with timerTextObject. You don't even need timerText.
     
    marplast likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    So instead of having one field for the GameObject, and then one field for the component, and then calling GetComponent on the GameObject, you can just do this:

    Code (CSharp):
    1.     [SerializeField] Text timerText;
    There is no need to call GetComponent, you already have a reference to the Component. Just drag your the object with the Text component into the inspector and you're good to go.
     
    marplast and Brathnann like this.
  4. marplast

    marplast

    Joined:
    Jun 21, 2020
    Posts:
    15
    Thanks both for the answer. I see what you mean and have fixed the problem.

    Just as a general rule, will this be the most efficient way of referencing another object (as opposed to FindObjectOfType()) ?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Yes - this is pretty much the most efficient way possible in Unity. It's a direct reference baked into the scene/prefab.

    FindObjectOfType is not even a similar concept. It searches every single object in the scene until it finds an object of a particular type, and just grabs the first one it finds. This is extremely slow, and there is no way of disambiguating if you have more than one object of the same type.
     
    marplast likes this.
  6. marplast

    marplast

    Joined:
    Jun 21, 2020
    Posts:
    15
    That's great Praetor - this is useful.