Keeping player active.

The problem with the code is that the console is picking up the ‘E’ input multiple times per frames instead of only picking it up once. Basically, the code, when clicking ‘e’ will make the character smaller and I want it to stay like this, but it is flickering between large and small. As well the white blocks by the teleporters not setting active again after ‘E’ is pressed a second time.
Can anyone assist with this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PowerUp : MonoBehaviour
{
    private bool isSmall = false;
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey (KeyCode.E))
        {
            Debug.Log("E WAS PRESSED");
            checkForSize();
         
        }
    }
 
    private void checkForSize()
    {
         if (isSmall == false)
            {
                transform.localScale = new Vector3(1f,0.5f,1f);
                isSmall = true;
                Debug.Log("BECOME SMALL");
                GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("TeleportBlock");
                foreach (GameObject go in gameObjectArray)
                {
                    go.SetActive (false);
                    Debug.Log("Objects GONE");
                }
            }
            else
            {
                if (isSmall == true)
                {
                    transform.localScale = new Vector3(1f, 1f, 1f);
                    isSmall = false;
                    Debug.Log("BECOME BIG");
                    GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("TeleportBlock");
                    foreach (GameObject go in gameObjectArray)
                    {
                    go.SetActive (true);
                    Debug.Log("Objects BACK");
                    }
              
                  
                }
            }
            // CODE BAD FIND A WAY TO RUN E OUTSIDE UPDATE AS IT IS
            //RUNNING TOO MANY TIMES AND IF CAN REMOVE DELAY AND PLACE CODE BACK IN INPUT
    }
}

This is as the docs say: https://docs.unity3d.com/ScriptReference/Input.GetKey.html

If you only want to listen for input for one frame, you would use GetKeyDown instead: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

No, it runs once per frame but it runs consecutively over as many frames as the button remains pressed. At 60 fps each Update „lasts“ for 0.016666 seconds - no human can easily push and release a button within that tiny time window.

Input.GetKey(Keycode.E) is your problem.

GetKey = Continuous press
GetKeyDown = Once When Pressed
GetKeyUp = Once When Released.

Change to GetKeyDown to only call once per button press.

Thanks heaps for this… Makes better sense now.

Alternatively if you want to use the new input system’s direct workflow:

using UnityEngine.InputSystem;

Keyboard.current[Key.Space].isPressed;
Keyboard.current[Key.Space].wasPressedThisFrame;
Keyboard.current[Key.Space].wasReleasedThisFrame;