How do I use the same script on two objects correctly?

So I have two buttons that I want to basically do the same thing, just with minor differences. So I created “Button 1” and “Button 2” for example. Here’s my script which I will attach to both.
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;

public class ScriptName : MonoBehaviour {

    public string buttonPressed;

    public GameObject button1; //I am declaring which button goes where-

    public GameObject button2; //in the editor, not in the script.

    public void ButtonPress() //Gonna use this function for the button press.
    {
        if (this.name == "Button 1")
        {
            button2.GetComponent<Button>().enabled = false;
            buttonPressed = "Button 1"
        }
        if (this.name == "Button 2")
        {
            button1.GetComponent<Button>().enabled = false;
            buttonPressed = "Button 2"
        }
    }
}

Now SUPPOSEDLY when you mash button 1, button 2 disables and the string is set to button 1. Or when you hit 2, 1 disables and string set to button 2. But BOTH disable(?) when I hit EITHER button. I tried adding ‘else if’ to the second if statement but no luck, pressing button 1 would have the same effect(both disable), but hitting button 2 only disables button 1.

I just now realized an error, I may have to use a dummy object to store my string variable so that it isn’t stored twice, different for each button.

However I still do have the issue of not disabling/enabling buttons correctly.

In theory, clicking button 2(b2) should only run that script as b2, and this.name would only equal button 2, so it can’t run button 1’s if statement, disabling b2. How is b2 running b1’s statement if it’s name is button 2?

Might a dummy object help with this as well?

As you have guessed, having this script attached to the 2 buttons does not make sense since you will have the buttonPressed variable in two different locations, with two different values

  1. So attach this script to only one game object, remove it from your buttons, and attach it to another gameObject (the parent of your buttons for instance).

  2. Remove the callbacks you have specified in the onClick event of the buttons

  3. Change your script as follow:

    public class ScriptName : MonoBehaviour
    {

    // Drag & drop your 2 (or more) buttons in the inspector
    [SerializeField]
    private Button[] buttons;
    
    // Gives the index of the selected button in the `buttons` array
    // -1 means no button is selected
    private int selectedButtonIndex = -1;
    
    // Following property not necessary
    // Could be used by another script somewhere
    // to retrieve the name of the button for instance
    public Button SelectedButton
    {
        get { return selectedButtonIndex >= 0 ? buttons[selectedButtonIndex] : null }
    }
    
    private void Start()
    {
        // Attach the callbacks of the buttons by script
        for( int i = 0 ; i < buttons.Length ; ++i )
        {
            int buttonIndex = i ; // Keep this line
            buttons*.onClick.AddListener( () => SelectButton( buttonIndex ) ) ;*
    

}
}

private void SelectButton( int buttonIndex )
{
selectedButtonIndex = buttonIndex;
// Disable all the buttons except the selected one
for( int i = 0 ; i < buttons.Length ; ++i )
{
buttons*.enabled = (i == selectedButtonIndex);*
}
}
}