[SOLVED] Could someone explain this RefrenceError?

For some reason, my code editor is giving me this:
RefrenceError: position is not defined

For context, this is my code:

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) {
    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("EnemyShip");
        var instance = templateAsset.resource.instantiate();

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

    };

    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.entity.translate(0, 0, -0.5);
};

@Izzychief Hi and Welcome! Have a look to your code. At line 22 the variable position is used for the first time. On line 29 is when it is defined so this is what the error is your code and not knowing what the rest will do I can only make a suggestion here. If you move line 29 up to line 12 it will not give to this error. I don’t know your project but I think it may be also better to move 26 and 30 up but would have to take a look at a project.

Now I’m getting this:

TypeError: Cannot read properties of undefined (reading 'getPosition')

@Izzychief I don’t see this.entity populated anywhere. How do you get this.entity? What is this script attached to? Is it possible to share your project?

Sure, idk how though.

@Izzychief When you are in the editor for your project there should be a url in the top edit box of your web browser. In most cases if you click on it once it will highlight the entire link. If not you can just swipe in the url using your mouse button and drag over it. With it highlighted right click mouse and select copy. Once it is copied paste the link into the forum post by writing another message.

Here you go, but please only change what you need to.
https://playcanvas.com/editor/scene/1752496

Hi @Izzychief!

Without being a member of the project, other users cannot make any changes. Only they can see the project and eventually make a copy of the project by forking it.

@Tirk182 Ok, now you can change stuff.

@Izzychief I have no plans on changing any of your project. This is your responsibility. The forum is here to help you correct code and answer questions.

Thanks for the help! I fixed the errors but it’s now working as intended so I’ll have to work on it some more.

@Izzychief A brief description on the issues I have seen. First the script itself is attached to an entity which does not change it’s position. So the position snapshot inside the update loop will always have the same value. I don’t know for sure what you would like to do their. An entity like this will not be movable because it does not have a collision or rigid body. This is the reason that setPosition will not work but the getPosition will. Also, translate is only usable on your instantiated object.

In your code what happens every time the spawn timer expires is that a new object is instantiated from the asset “EnemyShip”. That object is then added to the root and it’s new random position on the x axis is translated. Here is my version. I don’t have a feel for your distances you want to generate using random so you will have to work that part out.

// update code called every frame
EnemySpawner.prototype.update = function (dt) {
    var position = this.entity.getPosition();
    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("EnemyShip");
        var instance = templateAsset.resource.instantiate();

        this.app.root.addChild(instance);

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

        instance.translate(randomspawn, position.y, position.z);

    };

};

I think this should solve your issue on this topic. If this is OK it can be marked as closed. Also, I would suggest that you create a new post if you have a new question.

It works now! Again, thanks for your help.