[SOLVED] Won't spawn enemies

How come this isn’t spawning more "EnemyCraft"s? The script has been saved, it is on an empty entity, it is visible by the camera

// initialize code called once per entity
EnemySpawner.prototype.initialize = function() {
    // Set the initial time
    this.time = 0;

    // Set the time for the next enemy spawn in seconds
    this.spawnNext = 1;
};

// update code called every frame
EnemySpawner.prototype.update = function(dt) {
    var randomspawn = (Math.random() - 0.5) * 2;
    // Set Random Position
    var position = this.entity.getPosition(); 
    this.entity.setPosition(randomspawn * 20, position.y, position.z);
    this.time =+ dt;

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

        // Spawn Enemy
        var templateAsset = this.app.assets.find("EnemyCraft");
        var instance = templateAsset.resource.instantiate();

        this.app.root.addChild(instance);
        instance.translate(position.x, position.y, position.z);
    }
};

Hi @TurtleBoi! Line 16 of your code above should be this.time += dt; instead of this.time =+ dt;.

Thank you! That fixed it!

Great! Did you write the code yourself or is it part of an example project you use?

everyone asks me that, may I ask why you did?

Because I see a lot of similar mistakes of different users. That could mean there is a wrong example somewhere. :upside_down_face:

ohhh, ok makes sense. im basic i guess lmao, i am a beginner so that may be why

No problem. If there is a wrong example somewhere, we would like to fix the example as well.

no example here

1 Like