Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

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

  1. TheProGamer01

    TheProGamer01

    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,722
    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,589
    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.