- Home /
is it good practice to find game object at start if i already put it in inspector?
I need some advice.
while im writing my game script i just wondering if, is it good practice to find game object at start if i already put it in inspector? or it just wasting performance?
anyway, to not wasting so many performance i also using if before finding the game object at start.
i know it will not have too much different in performance. but the point i want to ask is, IS IT GOOD TO CONTINUE DOING LIKE THIS FOR NEXT WHEN I WRITING MY OTHER SCRIPT?
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UI_Character : MonoBehaviour
{
[Header("References")]
public RawImage face;
public TextMeshProUGUI levelTxt;
public GameObject targetLocked;
public GameObject frameA;
public GameObject frameB;
public GameObject shortInfo;
[Header("Values")]
public bool isLocked = false;
private void Start()
{
checkReference();
}
private void Update()
{
//Use the the reference and do something
}
private void checkReference()
{
if (face == null)
{
face = transform.Find("Face_Mask").transform.Find("Face").GetComponent<RawImage>();
}
if (levelTxt == null)
{
levelTxt = transform.Find("Level_Text").GetComponent<TextMeshProUGUI>();
}
if (targetLocked == null)
{
targetLocked = transform.Find("TargetLocked").GetComponent<GameObject>();
}
if (frameA == null)
{
frameA = transform.Find("FrameA").GetComponent<GameObject>();
}
if (frameB == null)
{
frameB = transform.Find("FrameB").GetComponent<GameObject>();
}
if (shortInfo == null)
{
shortInfo = transform.Find("shortInfo").GetComponent<GameObject>();
}
}
}
Answer by xxmariofer · Jun 16, 2021 at 02:38 PM
first, take into account that checking if a Unity Object is not null is not as simple as comparing if a Non-Unity object, obviously doing it with a bunch of objects in the start will not make any difference, but it can take 10x times longer to compare a unity object than a non unity one. if you want to read more on this topic check this post
https://forum.unity.com/threads/optimizing-null-check-null-vs-bool-out-functions.482118/
Second, if you are going to catch references at runtime anyway, why bother to make it public and drag and drop from the inspector?
Probably my best advice I can tell you is to try to avoid mixing canvas elements with gameobjects .... unity scripts should be as independant as posible, Im not sure what frameA or frameB are, but probably that script should work even if none of the objects existed already.
If you are interested in learning about programming patterns this site is a good start
Hi xxmariofer, thankyou to tell me some good advice.
I still dont understand why i should "avoid mixing canvas elements with gameobjects". frameA and B is an UI Image and i referencing it by getting game object in script to turn on and off the game object. targetLocked also UI Image and shortInfo is like tooltip. anyway since UI Image is canvas elements is it something wrong there?
There are plenty of posible options, and without exactly knowing exactly whats your code ishard to say, but using a pattern like observer looks like a good idea. There is nothing extrictly wrong, but you should try to when referencing objects to mainly be childs and componentes of that same object