0 votes

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 mainscript.gd file from the spawncube_script.gd file and print it on the screen.

link of my project:
https://dosya.co/krt8ki6lx480/run.rar.html

Godot version 3.2.3
in Engine by (47 points)

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.

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.

mainscript.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!!!


spawncubescript.gd


extends StaticBody
var v = Vector3(0,1,-40)
var node = load("res://scripts/mainscript.gd").new()
func _process(
delta):
if v.z > 5:
self.queuefree()
else:
v.z += 0.1
self.set
translation(v) func onAreabodyentered(body):
node.increase
score()

errors:

debugger tab:
Invalid set index 'text' (on base: 'Nil') with value of type 'String'.
0 - res://scripts/mainscript.gd:8 - at function: increasescore
1 - res://scripts/spawncubescript.gd:12 - at function: onAreabodyentered
Errors tab:
getnode: Node not found: scorelabel.

1 Answer

+1 vote
Best answer

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.

by (1,863 points)
selected by

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.

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.

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

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.