InputEventMouseButton called twice and the node "RichTextLabel" is null on the second call

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

Hi,
I have created “Node2d” object and “RichTextLabel” as its child. The Node2d.gd script contains the following code to print the “$RichTextLabel” on mouse click. For every single mouse press (not release only pressed) this method is called twice and and first time the “not null” and then in the second call its prints “null”. There are 2 bugs. I expect to call once on mouse press and it should not be null. What is wrong in the following code ?

func _input(event):
 	if event is InputEventMouseButton:
  		if $RichTextLabel:
			$RichTextLabel.add_text("hello")
			print("not null")
		else:
			print("null")
:bust_in_silhouette: Reply From: deaton64

Hi,
you’re not checking if the button is pressed, just if the event is a mouse button.

if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed():
:bust_in_silhouette: Reply From: bob333

Found the issue. I had set the script as Main scene and also added it to the autoload. That’s why it was receiving the mouse events twice. But i guess one of the instance was printing the $RichTextLabel node as null, due to internal implementation.