Else and If activated at the same time?

So before the bug, i was working on my project until when i came back to it a script was missing, so i placed it back including the public things you drag gameobjects and such into. I’m not sure if this is relevant but I feel the need to say this because it may be the main culprit.

Anyways, I was working on a project and one of the things I was working on was where a character would enter a certain village (with a collider and a OnTriggerEnter, with bools, holding data as well), and one of the buttons would appear and if you clicked on it you would go onto a village menu. I was learning while making this, and was amazed to find my concept to be perfectly working last night. I called it a day and closed unity (I also saved it, so that’s that)

But then this morning when I opened the project up again, I played it again to see my amazing baby-step accomplishment only to find that the button would not work as intended when clicked. The button would appear when my character is on top said village, but the button’s ‘Interactable’ state would always stay off (checked in inspector’s tab during play mode). I looked through the script and placed debug.logs to see if the collision and such had been working:

    void Start () {
        //objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;

        colorToFadeTo = new Color(0.5f, 0.5f, 0.5f, 0.5f);
        enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
        objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
    }
	
	
	void Update () {
        if (inBahpTown == true)
        {
            //Gets variable "recruits" and assigns variable to variable "recruitCard"
            //recruitCard = objectholdingTown_Data.GetComponent<bahpTown_Data>().recruits;

            colorToFadeTo = new Color(1f, 1f, 1f, 1f);
            enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
            titleCard = ("Bahp Town");
            infoCard = ("You currently have " + relationCard + " relations with this " + rankCard + ". It is also " + wealthCard);
            relationCard = 0;
            rankCard = "Town";
            wealthCard = "Poor.";
            objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = true;
            Debug.Log("You are touching the town.");


        }
        else
        {
            colorToFadeTo = new Color(0.5f, 0.5f, 0.5f, 0.5f);
            enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
            inBahpTown = false;
            titleCard = "Null";
            infoCard = "";
            //objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
            Debug.Log("You're not touching anything.");
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        //objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = true;
        if (collision.gameObject.tag == "bahp_town")
        {
            inBahpTown = true;
        }
            
    }
     void OnTriggerExit2D(Collider2D collision)
    {
        colorToFadeTo = new Color(0.5f, 0.5f, 0.5f, 0.5f);
        enterButton.CrossFadeColor(colorToFadeTo, fadeTime, true, true);
        inBahpTown = false;
        Debug.Log("Leaving location.");

        objectholdingexecuteLocalMenu.GetComponent<executeLocalMenu>().enterButton.interactable = false;
    }
}

I tried it again and I saw the log would continuously say “You’re not touching anything” as fully expected. Then when the character collided with the village’s collision box something bamboozled and confused me severely. Both debug logs would show up in this order:

You're not touching anything
UnityEngine.Debug:Log(Object)
You're touching the town
UnityEngine.Debug:Log(Object)
You're not touching anything
UnityEngine.Debug:Log(Object)
You're touching the town
UnityEngine.Debug:Log(Object)

Bug in action: Dropbox - clip0025.avi - Simplify your life

Both debug.logs from both if and else were playing at the same time! How is this possible? Is it actually playing both ifs and else? I need help, I really don’t want to remake this game all over again! I tried “else if (inBahpTown == false)” instead of “else”. I tried moving some things around and eventually I managed to fix the button problem. But that would leave the rest of my data left in the else statement which would be used later on in another script to be immediately changed to “Null” and 0, float and string wise.

Can someone point out the flaw in my script? If you need anymore information you could use to help me I’d be glad to tell you.

Thanks in advance.

IF and ELSE are never active “at the same time”. The only way for the debug log to happen is if they get activated alternatingly or - and this is what I think is happening here - there is more than one instance of the script.

There is only one place setting the bool to false (line 32 can be ignored as the variable is already false in that case). That place would print out “Leaving location”.

So I guess you have two instances of the script and only one of them is connected to a collider that actually sets the bool inBahpTown to true, while the one of the other instance stays false.

To check this, you can print out the instance id as well.

Debug.Log(gameObject.GetInstanceID());

PS: The video kinda supports my assumption. It seems there is one instance that is moved with your player and changes state when colliding and one instance that stays in the “false” state.