[SOLVED] Enemy spawner is not working

The enemy spawner in my game stop working all of a sudden, it was working fine until last week I just stop working, I’ve looked into it and tried to fix it but I still not working. Can you help me figure out why it’s not working.

// update code called every frame
EnemySpawner.prototype.update = function (dt) {
    this.time += dt;

    var randomspawn = (Math.random() - 0.5) * 2;

    // If 1 Second has Passed
    if (this.time > this.spawnNext) {
        this.spawnNext = this.time + 1;

        // Set Random Position
        var position = this.entity.getPosition();
        this.entity.setPosition(randomspawn * 20, position.y, position.z);

        // Spawn Enemy
        var templateAsset = this.app.assets.get(56441364);
        var instance = templateAsset.resource.initialize();


    }
};

Hi @Vapor_Trout and welcome! You are missing something after the last line of your code. You have to do something with instance.

Like this?

// Spawn Enemy
var templateAsset = this.app.assets.find(“EnemyPlayer”);
var instance = templateAsset.resource.instantiate(“EnemyPlayer”);

That’s the part you already have, but I see this part is not correct so you can try to replace it.

Based on the code I can see, I think you need to add something like below.

this.app.root.findByName('Root').addChild(instance);
instance.setPosition(this.entity.getPosition());
// Spawn Enemy
var templateAsset = this.app.assets.find('EnemyPlayer');
var instance = templateAsset.resource.instantiate();
this.app.root.findByName('Root').addChild(instance);
instance.setPosition(this.entity.getPosition());

That fixed the error that was popping up, but now how do I make it spawn in different/randoms areas. It only spawns once at the start.

I assume you have add the code in the statement that’s in the update function? Then it should already work like you mentioned.

Ok. I got the random spawn to work, now i need to enemy to disappear after the timer goes off on my game. Is there a way to do that, if not it’s ok.

Great!

It depends on your script where and how to do that.

I have a question/another problem, i tried making my enemy players shoot but i never got it to work.

I suggest to create a new topic for it. Please show what you have so far and the current result.

k thanks.