Can't have hit collider ignore button

Kinda of a newbie here, so I have a UI button (named “attack” and mybutton) that I’m trying to ignore the hit collider so it doesn’t DisableButton(); but every time it ignores it and hits the background and disables it anyways. How do I solve this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ArmyUI : MonoBehaviour
{
    public GameObject mybutton;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

            RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
            if (hit.collider != null)
            {
                if (hit.collider.gameObject.name == "enemy")
                {
                    EnableButton();
                }
                if (hit.collider.gameObject.name != "enemy" && hit.collider.gameObject.name != "attack")
                {
                    DisableButton();
                }
            }
        }
    }

    void DisableButton()
    {
        mybutton.SetActive(false);
    }

    void EnableButton()
    {
        mybutton.SetActive(true);
    }
}

Your if statement on line 25 will trigger on any gameobject not named enemy or attack. This includes some gameobjects you may not want it to, such as any objects in the background.

You probably want to specificy which ones you want to disable it, instead of those you don’t:

if (hit.collider.gameObject.name == "deselect") {
    DisableButton();
 }

P.S may I suggest using tags, this way you can have many types of enemies, buttons or other objects that will each be handled the same way without having to add extra clauses to your if statements because of the way they are named.

if (hit.collider.gameObject.tag == "Enemy")

Hope this helps!