Sprite error when attempting to follow

The problem code is where I try to get the enemy sprite to follow the player sprite but it says the enemy doesn’t exist though the sprite is on the screen. I have tried fixing this but it never works. Anyway to fix this?

Can you attach the code?

I would use the local variable “sprite” that’s in that on created loop. I think “Zombie” refers to the most recent zombie created which could cause some issues with your code since you have 7 seconds worth of pauses in your code.

1 Like

FWIW, you should post a share link of your game instead of a picture. It makes it much easier for everyone.

Yes, the issue here is using zombie in the on created instead of the sprite in the event. The reason this is failing is pretty subtle, though - the on created handler runs synchronously (without breaking for other events), so it must complete before the create sprite will return.

This means that the zombie variable isn’t assigned a value until after the on created event(s) for that kind have finished running, so using zombie in there will always refer to the previous zombie (and for the first time, it’s undefined, causing the error you are seeing). When you use sprite instead there, it’s value will always be the sprite that has just been created

1 Like