Turning on and off collision by pressing a button

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By OpSyKi

I want to make a platformer that disable the usage of a platform by pressing a keyboard/controller button, pressing the button again enables the platform. The code I wrote made the collison turn off and immediately on again.
Take a look:

extends StaticBody2D

var red_c = true


func _process(delta):

    if Input.is_action_just_pressed("Action"):
	    if red_c == true:
		$CollisionShape2D.set_disabled(true)
		$AnimatedSprite.play("blue")
		red_c = false
		
		
		
    if Input.is_action_just_pressed("Action"):
	    if red_c == false:
		$CollisionShape2D.set_disabled(false)
		$AnimatedSprite.play("red")
		red_c = true
		
		
		
		
		
		
:bust_in_silhouette: Reply From: Adam_S

You should use an elif or else statement, also you don’t need to check twice for the same input.

if Input.is_action_just_pressed("Action"):
    if red_c == true:
        $CollisionShape2D.set_disabled(true)
        $AnimatedSprite.play("blue")
        red_c = false
    else:
        $CollisionShape2D.set_disabled(false)
        $AnimatedSprite.play("red")
        red_c = true