I cannot update the text of the label.

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

I’ve been trying to make a running game with simple shapes for a few days. I’ve done most of it, but the score is not updated when the character touches the cubes spawn on the screen. I am trying to increment the score variable in the main_script.gd file from the spawn_cube_script.gd file and print it on the screen.

link of my project:

Use excerpts of code, rather than linking your entire project. Edit it into your question so people can just look at the relevant bits rather than having to dig through a whole project.

psear | 2021-02-06 15:50

I am sorry I could not fully explain my problem. My English is not good. The error in the project sounds like the link between the two scripts.

main_script.gd

extends Spatial
var obj = load("res://scenes/cube.tscn")
onready var label = $score_label
var score = 0
func increase_score():
	global.score += 1
	label.text = "SCORE " + str(global.score)#ERROR SECTION!!!

spawn_cube_script.gd

extends StaticBody
var v = Vector3(0,1,-40)
var node = load("res://scripts/main_script.gd").new()
func _process(_delta):
	if v.z > 5:
		self.queue_free()
	else:
		v.z += 0.1
		self.set_translation(v)

func _on_Area_body_entered(_body):
	node.increase_score()


errors:

debugger tab:
Invalid set index ‘text’ (on base: ‘Nil’) with value of type ‘String’.
0 - res://scripts/main_script.gd:8 - at function: increase_score
1 - res://scripts/spawn_cube_script.gd:12 - at function: _on_Area_body_entered
Errors tab:
get_node: Node not found: score_label.

SDGN16 | 2021-02-06 16:39

:bust_in_silhouette: Reply From: deaton64

Hello,
I don’t quite have time to find the error, but I think it’s because you’ve set the main script as a singleton and then run the main scene.
Create a script and call it gloab.gd and put the score var in that, along with any others you need. The load that script as a singleton, delete the main_script.gd from the Autoload section.
That way, your script will load once.
Somewhere, the label is being set to a null, so when you try to set the text, it crashes.
If you can’t find or anyone else doesn’t reply, I’ll take a look tomorrow.
It helped uploading the project I think.

I tried what you said but got this error:
Error listening on port 6007
i’m new and can’t really solve these problems. please see my file.

SDGN16 | 2021-02-06 18:14

Hi,
OK, try this: should work now, no errors.

I’ve created a global script and added to vars - score & main.
In the main_script.gd I’ve set global.main to self so when you call the increase_score() function from spawn_cube_script() you won’t get the error.

I had to decrease gravity to 7, so I could bounce to the next platform.
I also added a find_node for the score label. That way, if you move the label, you won’t have to change your code.

deaton64 | 2021-02-07 11:25

thank you so much. but the link you sent is not working.

SDGN16 | 2021-02-07 11:35

Hi,
OK, try this: should work now, no errors.

deaton64 | 2021-02-07 11:58

thank you so much. It will also help beginners like me. :slight_smile:

SDGN16 | 2021-02-07 19:38