Switching items - can't detect a crazy loop that is happening.

So, I’m trying to switch weapons in my game. The player has 3 different ones. Each weapon has an ID that I use to detect wich weapon is active and I call the method to switch weapons when the player presses the “X” key.

if (Input.GetKey(KeyCode.X))
            {
                ChangeWeapons();
            }

This is the method (please, ignore the horrible hard coded parts haha.)

void ChangeWeapons()
    {

        if (mCurrentWeapon == 1 | mCurrentWeapon == 0)
        {
            mCurrentWeapon++;

        }
        else if (mCurrentWeapon == 2)
        {
            mCurrentWeapon--;
        }

        switch (mCurrentWeapon)
        {
            case 0:
                mWeapons[0].GetComponent<MeshRenderer>().enabled = true;
                mWeapons[1].GetComponent<MeshRenderer>().enabled = false;
                mWeapons[2].GetComponent<MeshRenderer>().enabled = false;
                break;
            case 1:
                mWeapons[1].GetComponent<MeshRenderer>().enabled = true;
                mWeapons[0].GetComponent<MeshRenderer>().enabled = false;
                mWeapons[2].GetComponent<MeshRenderer>().enabled = false;
                break;
            case 2:
                mWeapons[2].GetComponent<MeshRenderer>().enabled = true;
                mWeapons[0].GetComponent<MeshRenderer>().enabled = false;
                mWeapons[1].GetComponent<MeshRenderer>().enabled = false;
                break;
        }

    }

What is happening is a crazy loop that changes the weapons three or four times at once. I need to fix this. Any suggestions? Thanks for the attention in advance.

You might want to look here: Unity - Scripting API: Input

The ‘GetKey()’ method will return true whilst a key is held down, so unless you tap and release the key within the space of one frame (assuming you’re checking in the Update loop), you will be calling Change Weapon a lot.

You probably want to switch to GetKeyUp or GetKeyDown