Counter dosen't work

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

Hi, I have a problem with the spawner counter on a project that i work.

var sprite=load("res://Scene/Sprite.tscn")
var counter = 0


func _on_Button_pressed():
	var spawn = sprite.instance()
	add_child(spawn)
	counter =+1
	$Label.text = str(counter)

To be short it only show 1 and it get stuck there regardless how much I press the button. Any ideea?

P.S. Thanks in advance and sorry for bad english

:bust_in_silhouette: Reply From: Zylann

You made a typo:

counter =+1

This will set the value of counter to +1. It’s apparently valid code, cuz after all, +1 is a positive number. The + just happens to be optional, unlike - for negative numbers.

You should replace it with:

counter += 1

Which will increment the value of counter by 1.
(It’s a shorthand for counter = counter + 1).

Thanks you very much, i thought that is different from c++/c# the main reason been that i work from the book learn gotod in 24 hours and i saw there an incrementation that was write like that. I feel akward now… thanks again!

Shortanel | 2020-09-18 12:57