Want to make a script/object know when its being deactivated

hi, I want to make a script/object know when its being deactivated and before its being deactivated run its last line of code, i hope you understood. If not ask me.

There is the OnDisable function, which works as simple as this.

Whenever the object is disabled/toggled off, that function will be fired.

If you are deleting the object entirely, there is also OnDestroy, which also works just as simple as this.

Instead, whenever the object is called by Destroy(gameObject); that OnDestroy function is called.

Given, that both of these scripts/functions exist on the object you are affecting.

All you have to do is set a function to run before you deactivate the object.

Let’s say this first script will deactivate the second one.

GameObject theOtherGameObject;

void Update()
    if (Input.GetKey(KeyCode.Space))

Then on the second script (attached to theOtherGameObject) you define the function.

public void DisableThis()
    //Do whatever you want here
    gameObject.enabled = false;

So in this case, whenever the spacebar is pressed, it will run the DisableThis function which you can define yourself.
Hope that’s what you’re asking about!

Is it possible to make that “theOtherGameObjekt” is any game object that has a specific tag? Im using a raycast to find the object btw.