Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Do variables remain in memory after a script is disabled?

Discussion in 'Scripting' started by Berty_Bert, Sep 3, 2020.

  1. Berty_Bert

    Berty_Bert

    Joined:
    Feb 12, 2020
    Posts:
    3
    public class Text : MonoBehaviour
    {

    private int num = 1;
    public GameObject Button;


    void Awake()
    {
    if(num = 1)
    {
    enabled = false;
    }
    }
    }

    // Suppose I reference the gameObject "Button" through the inspector....will the variables "Button" and "num" still remain in memory or will they be deleted?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Yes, the entire component and all of its data remain in memory no matter what unless the component is Destroyed.
     
    Sab_Rango likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    Loose references only get collected when there is no connection to them anymore. Disabling an object just tells Unity to not run functions (such as Update) for that object. It still very much exists. So yes, the state of the object remains unchanged when you disable and enable it.
     
  4. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    To be even more specific. If a game object is disabled (or just a script/component) on the game object, no Awake/Start/Update/FixedUpdate/LateUpdate/OnGUI/OnEnable/OnDisable function in your script will run. Your variables will keep their assigned values however.