I made 3 scenes to practice switching scene, I can switch from 1 to 2, from 2 to 3, from 3 to 2, ect… by clicking on keys. My issue is that once I go back to the first scene I CANNOT switch scene anymore, the script doesn’t even execute anymore, it is like I am on my Scene 1 but my script is not attached, however as long as I don’t go to scene 1 I can switch from 2 to 3 and from 3 to 2 unlimited times.
Here’s the code used:
Code used for scene 1:
extends Node2D
func _unhandled_key_input(event):
var level_2 = preload("res://Levels/level_2.tscn")
print(event.as_text())
if event.is_action_pressed("Next"):
get_tree().change_scene_to_packed(level_2)
Code used for scene 2:
extends Node2D
func _unhandled_key_input(event):
var level_1 = preload("res://Levels/level_1.tscn")
var level_3 = preload("res://Levels/level_3.tscn")
print(event.as_text())
if event.is_action_pressed("Previous"):
get_tree().change_scene_to_packed(level_1)
if event.is_action_pressed("Next"):
get_tree().change_scene_to_packed(level_3)
Code used for scene 3:
extends Node2D
func _unhandled_key_input(event):
var level_2 = preload("res://Levels/level_2.tscn")
print(event.as_text())
if event.is_action_pressed("Previous"):
get_tree().change_scene_to_packed(level_2)
I reproduced your code locally. Check the bottom of the Editor while it’s running, look at the Debugger Errors, you’ll see a couple! I haven’t personally messed with scenes, but it looks like there’s some issues relating to switching scenes and perhaps which is your main scene (because when I made 2 my main scene, I saw the same behavior but couldn’t switch back off of 2).
E 0:00:02:0518 _parse_ext_resource: res://Levels/level_1.tscn:7 - Parse Error: [ext_resource] referenced non-existent resource at: res://scene1.gd
<C++ Source> scene/resources/resource_format_text.cpp:163 @ _parse_ext_resource()
E 0:00:02:0525 set_path: Another resource is loaded from path 'res://Levels/level_1.tscn' (possible cyclic resource inclusion).
<C++ Error> Method/function failed.
<C++ Source> core/io/resource.cpp:75 @ set_path()
Edit: Looks like it is a cyclic dependency. One way to fix it seems to be to use load here instead of preload.