RaycastHit return true all the time

When I look at some object and press the button, I need to do something. When I do it for the first time, it works, but then I don’t need to press the button again, I can just look at object. But player must look at object and press the button, not only look

private Collider thisCollider;
public int ActionNumber { get; private set; }

void Start ()
{
    thisCollider = GetComponent<Collider>();
}

void Update ()
{
    if (Input.GetButton("Fire1") && DoPlayerLookAtObject())
        ActionsList();
}

bool DoPlayerLookAtObject()
{
    // Ignoring player's collider
    int layerMask = 1 << 9;
    layerMask = ~layerMask;

    RaycastHit _hit;
    Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
    bool isHit = Physics.Raycast(_ray, out _hit, 2.0f, layerMask);        
    if (isHit && _hit.collider == thisCollider)
        return true; // this return true all the time after first interaction with object
    else
        return false;
}

public bool ActionsList()
{
    if (DoPlayerLookAtObject())
        switch (thisCollider.name)
        {
            case "barthender":   ActionNumber = 1; return true;
            case "doorToStreet": ActionNumber = 2; return true;
            default: Debug.Log("Error: Out of range"); break;
        }
    return false;
}

How I use it:

public OnMousePressCasino onMousePressCasinoBarthender;
public OnMousePressCasino onMousePressCasinoDoorToStreet;

if (onMousePressCasinoBarthender.ActionNumber == 1 &&
    onMousePressCasinoBarthender.ActionsList())
    // do something

if (onMousePressCasinoDoorToStreet.ActionNumber == 2 &&
    onMousePressCasinoDoorToStreet.ActionsList())
    // do something

Ignoring player’s collider:

89480-1.jpg

When you are using it in this script you are not checking for the button anymore. It works like you want on the first time because the ActionNumber is 0, but after you set it on first time it will be 1 or 2 and these if-clauses are calling the ActionList() method again.

public OnMousePressCasino onMousePressCasinoBarthender;
public OnMousePressCasino onMousePressCasinoDoorToStreet;

if (onMousePressCasinoBarthender.ActionNumber == 1 &&
    onMousePressCasinoBarthender.ActionsList())
    // do something
 
if (onMousePressCasinoDoorToStreet.ActionNumber == 2 &&
    onMousePressCasinoDoorToStreet.ActionsList())
    // do something

It is little hard for me to understand what you are trying to accomplish. Now you have two different scripts calling ActionList() method. First one calls when you press button and player is looking at something. Second one calls when ActionNumber is 1 or 2.