game broke after changing main scene

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

i have been working on an action rpg and have just finished the title screen, however, whenever i get hit by an enemy or i hot an enemy, the game crashes. i have figured out what is wrong, but i dont know how to fix it. i have made the hit effect appear on the main scene, but my title screen is now the main scene. how do i make it appear on the current scene?

Thanks!

You should share the relevant code, otherwise it will be difficult to determine what you’ve done wrong.

Nisovin | 2021-03-01 05:25

sorry, i should have done that. this is the code that has the problem:

extends Area2D

const HitEffect = preload(“res://Effects/HitEffect.tscn”)

var invincible = false setget set_invincible

onready var timer = $Timer

signal invincibility_started
signal invincibility_ended

func set_invincible(value):
invincible = value
if invincible == true:
emit_signal(“invincibility_started”)
else:
emit_signal(“invincibility_ended”)

func start_invincibility(duration):
self.invincible = true
timer.start(duration)

func create_hit_effect():
var effect = HitEffect.instance()
var main = get_tree().current_scene
main.add_child(effect)
effect.global_position = global_position

func _on_Timer_timeout():
self.invincible = false

func _on_Hurtbox_invincibility_started():
set_deferred(“monitorable”, false)
monitorable = false

func _on_Hurtbox_invincibility_ended():
monitorable = true

i added a title screen, which made that the main scene, thus the game tried to put the efect on that scene. i think that is what happened at least.

Redshellmeerkat | 2021-03-01 21:55

:bust_in_silhouette: Reply From: Emankcin

Without looking at what you’re doing it’s difficult to answer your question.

Here’s a way you can add something to the current scene:

get_tree().current_scene.add_child(hit_effect)