Creating a new Enemy after initial start not working

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

Link to screenshots (3):




I am trying to make an enemy spawner where it spawns an enemy once it detects the player, however, the game crashes immediately after it spawns the enemy. Also, the enemies spawn fine during the initial launch.

Please copy and paste your code in the post (try using the curly braces in the question editor).

Ertain | 2021-03-18 06:08

:bust_in_silhouette: Reply From: Surtarso

your nodes are not ready when the others try to see them.
add and autoload script with simply

var player = null
var enemy = null
var ui = null
var stage = null
var etc = null

and on you scenes add:
on your enemy script _ready(): your_autoload.enemy = self
on your player script _ready(): your_autoload.player = self
etc

and on all functions you need to interact with enemy, just add
if your_autoload.enemy != null: keep going with your function

and functions that interact with player
if your_autoload.player != null: keep going etc

this will avoid coupling errors and make it a lot easier to share information between nodes… instead of get_node() you can simply call your_autoload.enemy.position = etc etc

also make sure the order you add your children correspond to the order your nodes try to detect your children… you can’t call a function on a child that is not yet instanced

I like, instead of preloading, using:

export var Enemy_1:PackedScene

and load my enemy in the properties panel, this is just my preference to keep things more organized and avoid any error.

Thanks this helped a lot! :smiley:

DraegonCannon | 2021-03-30 01:16