'Previously freed instance' After restarting the game and trying to update the score func

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


Im Currently trying to work out how to make the game restart after players lives hit 0.
This ive managed to do. Game Over screen pops up and then i can hit restart.
The game then restarts From the beginning. Then i hit this issue once i either try to increase the score. or decrease life.

This is my current Restart button code

func _on_Restart_button_down():
    global.score = 0
    global.lives = 3
	get_tree().paused = false

Im Having no issue with the scene reseting but this error only happens when i try to do the following/ IT also happens when i press the pause button:

func updatescore():
	Score.text = str(global.score)
func updatelife():
	Life.text = str(global.lives)
	if global.lives == 0:
		get_tree().paused = true

The Above Updates the global score or life var which is nothing more than:

var score = 0
var lives = 3

The second error line is just this line here:


Ive searched around but im not sure how to fix this issue?
Do i need to free the GUI Scene? Then re add it?
Ive tried this but, But none of the methods Ive tried Have worked.

Basically. After i restart the game the vars should reset And should play the game again like you would expect. With score updating and so on. Any help would be appreciated Thank you

I Changed The restart button to this:

func _on_Restart_button_down():
	global.score = 0
	global.lives = 3

Then in my global:

onready var currentScene = get_node("/root/Main/GUI")

func restart(scenePath):
	#Free Scene
	#Instance Scene
	var addScene = load(scenePath)
	currentScene = addScene.instance()
	get_tree().paused = false

This Adds a GUI over the top which means the pause button now works BUT it still wont update the score. It hits me with an error.

How can i remove the GUI From The Tree so when its re instanced it works?

Sounds like if you are setting Score.text on a deleted node. Make sure it exists at the point of calling the function.

Soaku | 2018-09-22 07:32

Can u please show me how you solved this Archtects.
I also have same problem in my project.Please.

THE HELIX | 2019-02-13 15:00

:bust_in_silhouette: Reply From: ArdaE

It looks like Score and Life point to nodes that exist in the scene you’re reloading, hence end up pointing to freed nodes after you reload your scene. I assume you set them via onready or within your _ready function. Try setting them again after reload, or access the nodes they point to via their node path each time (e.g. via $…), and the problem should go away.

Thank you, This makes so much sense, and kind of annoyed by my self that i didn’t think about it!

Archtects | 2018-09-25 07:26