Do variables remain in memory after a script is disabled?

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?

Yes, the entire component and all of its data remain in memory no matter what unless the component is Destroyed.

1 Like

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.

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.