- Home /
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 =)
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.
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.
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 : $$anonymous$$onoBehaviour
{
//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);
}
}
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. =(
Answer by Vivaldin · Jun 25, 2017 at 03:22 PM
Woked =)
Using Cat = GameObject.Find("Cat"); in void start()