How do you undo a variable on a script attached to an object?

I’m trying to undo a variable which I have stored on a script attached to a game object. However, when I attempt an undo, it doesn’t even register the undo.

Here is the pseudo code I typed up:

SudoCode.cs

using System;
using UnityEditor;
using UnityEngine;

public class SudoCode : MonoBehaviour
{
    public int intSomething;

    public void SetToOne() //called from an inspector button push
    {
        Undo.RecordObject(this.transform, "Change intSomething");
        intSomething = 1;
    }

    public void ChangeInt() //called from an inspector button push
    {
        Undo.RecordObject(this.transform, "Change intSomething");
        intSomething = 2;
    }
}

SudoInspect.cs

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(SudoCode))]
public class SudoInspect : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        SudoCode sudoi = (SudoCode)target;
        if (GUILayout.Button("Set to 1"))
        {
            sudoi.SetToOne();
        }
        if (GUILayout.Button("Set to 2"))
        {
            sudoi.ChangeInt();
        }
    }
}

I’ve also tried this.gameObject on lines 11&17 of SudoCode.cs, but it didn’t make a difference. I also tried Undo.RegisterUndo, with this, the undo registered, but it didn’t undo the variable change, also it is depreciated. =/

I figured out the problem, and now I feel silly. The correct code is Undo.RecordObject(this, “Change intSomething”)

I thought the undo functioned needed gameObjects, and couldn’t directly call the script itself, but this was incorrect. The undo functions need ‘Objects’ which nearly everything in unity inherits from, even scripts do, in a roundabout way. That is why ‘This’ works for Undo.RecordObject, and This.gameObject didn’t do what I thought it would. Because I needed to reference the script in order to save the variables from it, NOT the gameObject which the script is attached to.

Hopefully, if anyone else runs into this problem, they will find this right away, instead of spending days looking for the info like I did. =P