• 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
Question by Vivaldin · Jun 10, 2017 at 09:39 AM · unity 2doptimizationstaticfindreferencing

Unity 2d static variables versus GameObject.Find("")

Evening, gentleman.

I got a number of gameobjects that do stuff. Each got a script that calculates some variables, that i need to use in other scripts. Like, controlling my player with a joystick. Im calculating the control variable in the joystick script. Than i parse the script with GameObject.Find("") from the script on the player, looking for this variable and use it.

As i go, my project ends up using alot of Find functions and referencing lots of scripts and gameobjects. It appears to be that GameObject.Find("") needs to be put in update(), so i guess the system is searching the memory for all this number of gameobject each frame. I suppose that these searches take monstrous amounts of computing time.

So, the question is how to arrange everything the most optimal way? Does the GameObject.Find("") uses much more operations? Is using simple static variables better? Or is it even better to create 1 super-master script with everything inside it, so there will be no need for statics, or any kinds of searches and referencing at all?

Thnx 2 all =)

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by sleepandpancakes · Jun 11, 2017 at 01:03 AM

Do not use GameObject.Find(). This is probably the worst and slowest way you could reference your other objects.

Do not make 1 super-master script. This will make your code unorganized, prone to errors, and almost impossible to work with. Each script should only do one basic thing. For example, your PlayerMovement script should handle player movement and only that.

The easiest way to reference other components/objects is to make a public variable in your script and assign the reference in the inspector. If you need to find objects during runtime, GetComponent() and similar functions may be helpful. But more importantly, you should cache your reference instead of finding it every frame on Update().

For example, if I have a Dog object that is supposed to follow a Cat object, I will declare the Cat object inside the Dog class, and initialize it in Start() or Awake(), like so:

     public class Dog {
     
         public Cat cat;
 
         void Start()
         {
             cat = FindCat();
         }
 
         Cat FindCat()
          {
              //finds the cat
          }
         
         void Update()
         {
             Follow(cat); //note that we are not finding the cat in update, rather we are using our already initialized cat variable
         }
 
     }

This becomes even easier if we assign the reference via the inspector because then we don't need the FindCat() function.

Comment
Vivaldin

People who like this

1 Show 4 · 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 Vivaldin · Jun 14, 2017 at 11:31 AM 0
Share

But, if i reference the cat in the Start area, if the cat changes its position - it will not be updated? Am i right?

If a cat is in one place - i can get its positions once. But it the cat runs away - i need to track its positions every frame.

avatar image eskivor Vivaldin · Jun 14, 2017 at 01:39 PM 0
Share

You have to get the reference of the object only once. If you want to get a variable parameter (ex : the position) of the object, do it when the variable is updated.

For example :

 using UnityEngine;
 
 public class GetCatPosition : MonoBehaviour
 {
     //Use "[SerializeField]" or "public" if you want to get the reference in the inspector
     [SerializeField]
     Transform cat;
 
     void Start ()
     {
         GetCatReference ();
     }
 
     //If you want to get the reference via the script
     void GetCatReference ()
     {
         //do it where the cat is, with findobjectoftype, getcomponent, etc.
         cat = GetComponent<Transform> ();
     }
 
     void Update ()
     {
         //You don't have to get the cat reference here
 
         //The line just below is useless here, you have already the reference
         //cat = GetComponent<Transform> ();
 
         //Example : Print the current position of the cat
         Debug.Log ("cat position " + cat.position);
     }
 }


avatar image Vivaldin eskivor · Jun 23, 2017 at 03:43 PM 0
Share

The problem is, that when i put the object into prefabs and than take it back in the the scene - all via inspector references are empty and need to set them again by hand each time. =(

Show more comments
avatar image

Answer by Vivaldin · Jun 25, 2017 at 03:22 PM

Woked =)

Using Cat = GameObject.Find("Cat"); in void start()

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Static variables optimization 2 Answers

Find, Findwithtag, findgameobjectwithtag Not working 3 Answers

Why i am having fps drops in Moto G5 S plus ? 0 Answers

General programming question on Static Classes 1 Answer

Is there a more efficient way to write a "Find" script? 2 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