Question by
yukisoulme · Jun 17, 2021 at 01:28 PM ·
garbage-collection
what will happen to the reference that i create inside method if i no longer use it?
Just wondering what will happen to the reference(Game Object, Transform) that i create inside method if i no longer use it?
is it will be stuck in memory? or unity will release it automatically?
if referencing like 1st way will increase garbage collection then doing it 2nd way will be better?
can somebody convincing me with my thoughts?
this is my script and my hierarchy pic :
using UnityEngine;
public class Test : MonoBehaviour
{
[Header("Reference")]
public MapManager mapManager;
public Map_Cities mapCities;
private void Awake() //or Start
{
//Find MapManager - 1st Way
GameObject gameManager = GameObject.FindWithTag("Manager"); //what will happen to this "GameObject gameManager"
Transform mapManagerTransform = gameManager.transform.Find("MapManager"); //what will happen to this "Transform mapManagerTransform"
mapManager = mapManagerTransform.GetComponent<MapManager>();
//Find MapManager - 2nd Way
//mapManager = GameObject.FindWithTag("Manager").gameManager.transform.Find("MapManager").GetComponent<MapManager>();
//Find Map_Cities from mapManager
mapCities = mapManager.map_Cities;
}
private void Update
{
//use refrence to do something as example
}
}
screenshot-2021-06-17-215306.png
(59.1 kB)
Comment
Your answer
Follow this Question
Related Questions
garbage collection in between scene changes? 2 Answers
UnloadUnusedAssets causes GC.MarkDependencies to spike 0 Answers
Efficiency when declaring variables 3 Answers
WebGL not releasing memory,WebGL not Releasing the memory 0 Answers
How to traverse a dictionary without foreach that creates boxing? 2 Answers