Instantiating help

Godot Version

4.2.1

Question

I’m trying to spawn in enemies at random intervals by instantiating the enemies in a timer node. I was following this tutorial:

The only thing I did differently was the positioning, and I did the code in a timer node instead of a node 2D. Whenever I try to run the project, it gives me the error “Invalid call. Nonexistent function ‘instantiate’ in base ‘GDScript’.” The only thing I can think of being wrong is that something changed in GDScript because it appears that his version is 4.1.

Can you show the codes?

Ok, I did actually fix it. The problem was that the nodes weren’t in the correct scene, so I managed to fix that.

However, I’m getting another error. The instantiated enemies can’t hit the player, and the left enemy can’t hit the player at all. so if you could help with that, that would be great.

Here’s the nodes the enemies use:

Here’s the code for the enemies:

Here’s the errors I’m getting:

I’m also getting this alert, but I’m not sure what it means:

It would be better to check the body by name or type, not by node reference

if body.name == "Player":
    print("hit player by name")

# if you set a class_name Player
if body is Player:
    print("hit player by type")

Make sure to paste code instead of screen shots

2 Likes

This worked, thanks!

Unfortunately, these forums are blocked on my laptop, so I have to use my phone, and it’s easier for me to take a picture instead of typing out the code.