Label.text = "athing" isnt working. PLEASE HELP!

my script isnt working because of the label. It is not working its just keeps on giving me a error: Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘null instance’. I am trying to make it so when either player one or player 2 wins the game its shows who won on another scene. Code: `extends Node2D

@onready var player_1s_points: Label = $“Player 1s Points”
@onready var player_2s_points: Label = $“Player 2s Points”
@onready var title: Label = $Title

func _ready() → void:
if get_tree().current_scene and get_tree().current_scene.name == “Game”:
await get_tree().create_timer(0.1).timeout
Global.P1_Points = 0
Global.P2_Points = 0
player_1s_points.text = str(Global.P1_Points)
player_2s_points.text = str(Global.P2_Points)

func _process(delta: float) → void:
if Global.P1_Points == 9:
_winP1()
elif Global.P2_Points == 9:
_winP2()

func _winP1():
get_tree().change_scene_to_file(“res://Scenes/win_screen.tscn”)
Global.P1_Points = 0
Global.P2_Points = 0
title.text = “PLAYER 1
HAS WON!”

func _winP2():
get_tree().change_scene_to_file(“res://Scenes/win_screen.tscn”)
Global.P1_Points = 0
Global.P2_Points = 0
title.text = “PLAYER 2
HAS WON!”
`

Github: GitHub - kaihan-man/PongMine: Another Pong game this is the right one.

The problems happens because you don’t have any node called Title in your scene:

image

1 Like

yeah, its in the End screen scene. How would i make it so it works then?

I played around and you can fix it with the following changes.
In your game.gd script do this:

extends Node2D

@onready var player_1s_points: Label = $"Player 1s Points"
@onready var player_2s_points: Label = $"Player 2s Points"

func _ready() -> void:
	if get_tree().current_scene and get_tree().current_scene.name == "Game":
		await get_tree().create_timer(0.1).timeout
		Global.P1_Points = 0
		Global.P2_Points = 0
		player_1s_points.text = str(Global.P1_Points)
		player_2s_points.text = str(Global.P2_Points)

func _process(delta: float) -> void:
	if Global.P1_Points == 9:
		Global.won_player = 1
		load_win_screen()
	elif Global.P2_Points == 9:
		Global.won_player = 2
		load_win_screen()

func load_win_screen() -> void:
	get_tree().change_scene_to_file("res://Scenes/win_screen.tscn")

In your Global.gd script:

extends Node

var P1_Points = 0
var P2_Points = 0

var won_player: int = -1

In your win_screen.gd script:

extends Node2D

@onready var winner_sfx: AudioStreamPlayer = $WinnerSFX

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	winner_sfx.play()
	Global.P1_Points = 0
	Global.P2_Points = 0
	
	$Title.text = "PLAYER %s
	HAS WON!" % Global.won_player
	
	Global.won_player = -1
2 Likes