Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yukisoulme · Jun 16, 2021 at 12:46 PM · performance optimizationadviceask

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>();
         }
     }
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

https://www.habrador.com/tutorials/programming-patterns/

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yukisoulme · Jun 17, 2021 at 12:55 AM 0
Share

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?

avatar image xxmariofer yukisoulme · Jun 17, 2021 at 07:09 AM 0
Share

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

avatar image yukisoulme xxmariofer · Jun 17, 2021 at 12:46 PM 0
Share

oh ok. thanks anyway. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

need some suggestions on what to add in game 1 Answer

The best way to trigger game events in a list 1 Answer

Getting input from external Files, advices? 1 Answer

How and when to use a JS Array over BuiltIn Array 2 Answers

Used Textures stat.. what exactly does that mean ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges