Problem with the score!

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

Hi everyone! I desperately need your help. I have three scenes: a player, a coin, and a Main, which contains both. My problem is to add a label that scores the points when the player collects the coin. I tried in every way, looking also in this section, but there is nothing to do! I just can’t start the score! Anybody could help me, please? :frowning:

could you share us the code?

p7f | 2018-12-03 18:33

What code? Currently, the only code I’ve added is the one of the answer below.

Rob1980 | 2018-12-04 18:28

:bust_in_silhouette: Reply From: Squatnet

Assuming you have a scene looking something like this?

  • Main
    – Label
    – Player
    – Coin
    — Area2D
    you could do a score like so
var score = 0 #hold the score
func addScore(val): # function to add the score
	score += val # add val to score
	$Label.set_text(score) # Set the text of label to score
func _on_Area2D_area_entered(): # when something enters area2D
	addScore(1) # add 1 to score

It doesn’t work! :_(

My scene is something like this:

  • Main

– Player

– Coin (Area2D)

– CanvasLayer

— Score

I tried to insert the function in the Score script, and addScore () first in the coin script, then in the Main script, but nothing happens. In which scripts should I enter the code?

Rob1980 | 2018-12-04 18:22

:bust_in_silhouette: Reply From: shield

onready var score = $"../CanvasLayer/Score"

func _on_body_entered(body): 
    if body == player:

var score = 0

func add_score(val):
    score += 1
    text = score

Thank you! :wink:

Rob1980 | 2018-12-08 16:53