Does it make sense in units like disposal units
Yes, some data types are not all.
Does this apply to value type variables (integers, vectors)?
No.
It depends on the type of variable.
This means that it does not apply to int , double , float , bool , Vector3 and Vector2 and other similar data types. It doesn't even apply to string , because already, string cannot be reused in C #. strings are immutable.
In fact, using int from a local variable, say in a while is faster than using an int declared as global.
* Examples of when you should declare a variable once and reuse it, or in your own words, recycle or reuse variables in Unity *.
Arrays
If the function contains an array and this function is often called.
void functionCalledVeryOften() { float[] playerLives = new float[5];
This allocates memory every time and can be solved by creating a global array and initializing it outside the function once. You can create a simple function that flushes data in an array to 0.
float[] playerLives = new float[5]; void functionCalledVeryOften() { for (int i = 0; i < playerLives.Length; i++) { playerLives[i] = UnityEngine.Random.Range(0f,5f); } }
Creating new objects :
Creating new objects requires resources and can cause problems on mobile devices. It depends on how often you do it.
The code below creates a GameObject (bullet), then attaches a Rigidbody to it, and then removes it. This happens every frame when a space is held and finally destroys the mark 10 seconds later.
void functionCalledVeryOften() { if (Input.GetKey(KeyCode.Space)) { //Create new Bullet each time GameObject myObject = new GameObject("bullet"); Rigidbody bullet = myObject.AddComponent<Rigidbody>() as Rigidbody; //Shoot Bullet bullet.velocity = transform.forward * 50; Destroy(myObject); } }
The code is invalid because it allocates memory every time a new GameObject is created and when a GameObject is destroyed, it also starts the garbage collector. This can slow down and cause hiccups in your game.
The solution to the above code is to combine objects. You can learn more about it here: Unity Object Combination Tutorial
An example of a simple fix for this with a global variable:
List<GameObject> reUsableBullets; int toUseIndex = 0; void Start() { intitOnce(); } //Call this function once to create bullets void intitOnce() { reUsableBullets = new List<GameObject>(); //Create 20 bullets then store the reference to a global variable for re-usal for (int i = 0; i < 20; i++) { reUsableBullets[i] = new GameObject("bullet"); reUsableBullets[i].AddComponent<Rigidbody>(); reUsableBullets[i].SetActive(false); } } void functionCalledVeryOften() { if (Input.GetKey(KeyCode.Space)) { //Re-use old bullet reUsableBullets[toUseIndex].SetActive(true); Rigidbody tempRgb = reUsableBullets[toUseIndex].GetComponent<Rigidbody>(); tempRgb.velocity = transform.forward * 50; toUseIndex++; //reset counter if (toUseIndex == reUsableBullets.Count - 1) { toUseIndex = 0; } } }
So, basically you create an object inside the function before the game starts, and then save the link in a global variable. Then you reuse the object that you created in the function, since its reference is stored in a global variable.
Instantiate
The Instantiate function is used to create a copy of the collection. The code below will instantiate the bullet, and then shoots at each frame until the space is held and finally destroys it 10 seconds later.
public GameObject bulletPrefab; void functionCalledVeryOften() { if (Input.GetKey(KeyCode.Space)) {
The code is invalid because it allocates memory depending on how many components are attached to the bullet assembly and how many child GameObject are under it. The solution also uses a combination of objects. Create a GameObject in a function, save the link in a global variable, and then reuse it. The solution is the same with the solution above.
In conclusion, the sample code does not apply to your question.
You can learn more about memory management in Unity here .