[SOLVED] Can't get a "this." variable from another script

So I want to get the this.ammo_glock variable in another script but no matter what I try, I either get the value of ammo_glock as not a number or undefined. Thanks in Advance

Glockammmopickup.prototype.initialize = function() {
     this.app.root.findByName('WeaponController').script.weapon = this.weapon;
    
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

Glockammmopickup.prototype.onTriggerEnter = function(entity) {
//this.ammo_glock = this.ammo_glock + 50;
  //  console.log(this.ammo_glock);
    //this.entity.destroy();
    this.weapon.ammo_glock = this.weapon.ammo_glock + 50;
 this.app.root.findByName('WeaponController').script.get('weapon');
 console.log(this.weapon.ammo.ammo_glock);

};

Hi @agent178,

So, you are almost there, what you need to include in your property access order is the script name (script type):

// entity -> script component -> script instance -> property
this.app.root.findByName('WeaponController').script.glockammmopickup.weapon = this.weapon;

In my Case the Property is “ammo_glock” and the script instance is “weapon” yet the result just says its not a number

Glockammmopickup.prototype.initialize = function() {
     this.app.root.findByName('WeaponController').script.weapon.ammo_glock  = this.ammo_glock;
    
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

Glockammmopickup.prototype.onTriggerEnter = function(entity) {
//this.ammo_glock = this.ammo_glock + 50;
  //  console.log(this.ammo_glock);
    //this.entity.destroy();
    this.ammo_glock = this.ammo_glock + 50;
 console.log(this.ammo_glock);

};

Can you share a project link to take a look?

I think it is correct, but probably you need to initialize this.ammo_glock in your Glockammmopickup with an initial number, like zero. Otherwise you can’t add 50 to undefined.

1 Like

https://playcanvas.com/project/702035/overview/redacted here is the Project link and initializing the variable made it so that it became its own separate variable

Good, from what I see it’s working fine now, your property is getting updated when you step on the ammo trigger.

To have your weapon.js script ammo_glock variable update as well, you need to call this line each time you pick up new ammo:

this.app.root.findByName('WeaponController').script.weapon.ammo_glock  = this.ammo_glock;

Thank You , It works Perfectly now

1 Like