Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+8 votes

For context, I was going through the tutorials for Alien Attack and everything seemed to work fine up until I got to the Enemy Spawner Scene. In the tutorial, they asked us to delete the enemy that we had started with in game scene. After I did that and added this code:

func spawn_enemy():

var spawn_positions_array = spawn_positions.get_children()
var random_spawn_position= spawn_positions_array.pick_random()

var enemy_instance= enemy_scene.instantiate()
enemy_instance.global_position = random_spawn_position.global_position
add_child(enemy_instance)

to have it spawn in different random location, but when I run the game, the enemy just disappeared and don't show up at all.

Did anyone encounter the same problem? Any ideas on why this might have happened or how to fix it?

Thank you :)

asked in CSC380Jan2024 by (3k points)
0

Check the 2D model. It must be some issue there since the code looks fine.

5 Answers

+3 votes

I would try to add the child to the scene before setting its position. Additionally, make sure the visibility of the child is set to true and try debugging the positions of the enemy instance and make sure that it is within the scope of the camera

answered by (2.1k points)
+3 votes

I did encounter the same problem but I don't know if it's the same. It's whether I made a minor misspell on a variable/method ( or put the correct $ sign in front of variable, correct path name when you load in...) or I put the speed for enemy too slow that it never reaches the main screen

answered by (2.6k points)
+3 votes

I would say double check to see if you initialize the spawn_positions and spawn_positions_array using the @onready or _ready() function. Also check for misspelling variable names.

answered by (2.1k points)
+2 votes

instead of calling the function add_child() over here you should emit a signal (you can call it something like "enemy_spawned") and in the game.gd connect this signal and finally add the add_child() function in this function.

answered by (2.3k points)
+2 votes

Probably you have done that already but can double check that enemy's visibility property is set to true in its scene.

answered by (241 points)
...