Why isn't my scene gettnig changed to desired one ?

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

Hello guys ! I completed making stage one as well as almost finished Stage 2. However when I created an Area2D in the Stage 1 ending, on entering which the Stage 1 will get changed to Stage 2 something weird is happening ! The entire Stage 1 gets reloaded and the Area2D is working similar to my spikes that reload the whole scene. I am using this script in player :

func _on_Gotostage2_body_entered(body: Node) -> void:
	if "Player" in body.name:
		get_tree().change_scene("res://Stage2.tscn")

Due to some reason the scene gets reloaded instead of going to Stage 2.

Even If I directly use the above script in the Area2D (which I have named Gotostage2) then also the scene gets reloaded instead of going to Stage 2.

I even tried making the variable ‘TargetScene’ like this :

export(String,FILE,"*tscn") var TargetScene 

so that I can select the scene simply from the Inspector Window of Godot but still I am not getting the desired results. What can be the issue and how can I fix it ?

:bust_in_silhouette: Reply From: RazorSh4rk

I’m using this in my game where I wanna pass arguments to the next scene, maybe it’ll help you

var game = load('res://Main.tscn').instance() 
self.get_parent().add_child(game) 
self.get_parent().remove_child(self) 
self.call_deferred('free') 

But my script should also work and I am really anxious to know why and what is happening with it ? I saw some online tutorials also and they all are changing their scenes using the way I did ! And it’s working for them but for me the scene just gets reloaded.

Scavex | 2020-04-20 08:30

Yeah, but it’s really hard to tell just from this code. If your game isn’t secret, you can upload the project somewhere and i can take a look.

RazorSh4rk | 2020-04-20 08:36

No Problem, here is my project.

Scavex | 2020-04-20 09:09

KinematicBody2D.gd, line 101, remove get_tree().reload_current_scene(). Stage2 loaded up, but switched back to stage1, my guess is your VisibilityNotifier2D sent a signal when you changed scenes.

RazorSh4rk | 2020-04-20 09:48

Yep it worked ! But after doing this change my player wasn’t dying when he was falling from platforms. I fixed that by adding

if motion.y>1000:
		get_tree().reload_current_scene()

Thank you again !

Scavex | 2020-04-20 10:51