[SOLVED] Error i can't figure out

Hello, i have this error in last line playerToPrey is not defined but it is into initialize, i don’t get why

var Prey = pc.createScript('prey');
Prey.attributes.add('radius', {type:'number', default:7});
Prey.attributes.add('range', {type:'number', default:5});
Prey.attributes.add("preySpeed", {type:"number", default:0.5});
Prey.attributes.add("walkAnimCutoff", {type:"number", default:0.87});
Prey.attributes.add("blendTime", {type:"number", default:1});
Prey.attributes.add('Player', {type:"entity"});
// initialize code called once per entity
Prey.prototype.initialize = function() {
    var temp = new pc.Vec3();
    var playerToPrey= new pc.Vec3();
    this.fortune=5;
        this.attac=0;
        this.defence=0;
        this.damage=0;
        this.armor=0;
    this.model = this.entity.findByName("Model");
            this.startPosition = this.entity.getPosition();
            this.startRotation = this.entity.getRotation();
            this.originPrey = this.entity.getPosition();
            //this.enemySpeed = 1;
            this.moveTimer=0;
            this.newPreyPos= new pc.Vec3();
            this.lastPos= new pc.Vec3();
            this.entity.rigidbody.syncEntityToBody();
            //this.entity.collision.on('collisionstart', this.onCollisionStart, this);
            this.state = "idle";
};

// update code called every frame
Prey.prototype.update = function(dt) {
    this.moveTimer += dt;
            var playerPos = this.Player.getPosition();
            var playerState = this.Player.script.player.state;
            var preyPos = this.entity.getPosition();
            var distance = 0;
            //var newEnemyPos = null;
            playerToPrey.sub2(playerPos, preyPos);

Hi @ayrin,

That’s a local variable you declare in the initialize method. To be available in the update method most common way is to define it as a script property:

this.playerToPrey = new pc.Vec3();

You should use this.playerToPrey instead of var as var has scope within the function.

I see guys thanks, i will correct it, the strange thing is the same code worked inside the enemy script, i will change it too.

1 Like