Okay so i'm new to C# and i made this script. The goal i have in mind is that when the player is on the button and presses a button ( W in this case ) the button is switched from on to off ( or vice versa). The problem i have is that when the character stands on the button and presses W it doesn't always switch its state and i have no idea why. Any info would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Button : MonoBehaviour {
public bool Pressed = false;
Animator Anim;
// Use this for initialization
void Start () {
Anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
Anim.SetBool ("Pressed", Pressed);
}
void OnTriggerStay2D(Collider2D other){
if (Input.GetKey(KeyCode.W)){
if (Pressed){
Pressed = false;
} else {
Pressed = true;
}
}
}
}