Cannot access to variable from another script

As i have seen this is a very common topic, but none of the solutions have worked for me.
I am trying to access the variable colorsEnabled:

var NoLightColor = pc.createScript('noLightColor');

var colorsEnabled = true;

// initialize code called once per entity
NoLightColor.prototype.initialize = function() {

colorsEnabled = true;

 var app = this.app;
     this.entity.element.on('click', function() {
       this._onNoLightButtonActivated();
    }, this);

    //this.text1.element.text = this.explanation;

};

// update code called every frame
NoLightColor.prototype.update = function(dt) {

    console.log("cols is " + colorsEnabled);
    /*if(colorsEnabled){
        this.app.fire('object:colorsEnabled', this);
        //console.log("admitidos");
    } else {
        this.app.fire('object:colorsNotEnabled', this);
        //console.log("no admitidos");
    }*/
};

NoLightColor.prototype._onNoLightButtonActivated = function () {
    colorsEnabled = !colorsEnabled;
};

from the following script:

var GetLightColor2 = pc.createScript('getLightColor2');

GetLightColor2.attributes.add('palanca', {type: 'entity' });
//GetLightColor2.attributes.add('lightA', {type: 'entity' });
GetLightColor2.attributes.add("lightA", {type: "entity", array: true, title: "PanelLights"});
GetLightColor2.attributes.add('noColorButton', {
    type: 'entity',
    title: 'noColor'
});

//GetRotationLight.attributes.add('doorA', {type: 'entity'});//entity a la que queremos cambiar la textura

var angle = 0;
var percentageIntensity;

this.color1 = new pc.Color(1, 1, 1);
this.whiteLight = new pc.Color(1, 1, 1);
var areColorsEnabled;
var button;

 var app = this.app;

// initialize code called once per entity
GetLightColor2.prototype.initialize = function() {
    areColorsEnabled = true;
    button = this.noColorButton.script.noLightColor;
};

// update code called every frame
GetLightColor2.prototype.update = function(dt) {

    //areColorsEnabled = this.app.root.findByName('noColorButton').script.noLightColor.colorsEnabled;
    areColorsEnabled = button.colorsEnabled;
    console.log(button);
    //angle = this.palanca.getRotation().y;
    angle = this.entity.getEulerAngles().y;
    //console.log('an' + angle);
    //percentageIntensity = (angle - 0.499999999999997) * 10 /** 100*/ / 2;
    percentageIntensity = (angle + 90) / 180;
    //console.log('per' + percentageIntensity);

    this.colorChange(percentageIntensity);
};

and do not know why this.noColorButton.script.noLightColor is correct, but this.noColorButton.script.noLightColor.colorsEnabled is undefined

Thanks for your help

Hey, the reason why it shows undefined is because colorsEnabled variable is a global variable and not the script variable and you can simply access it by writing colorsEnabled as it is global.

In order to make it script variable, you will have to define it within script function with the script instance like

NoLightColor.prototype.initialize = function() {

this.colorsEnabled = true;

 var app = this.app;
     this.entity.element.on('click', function() {
       this._onNoLightButtonActivated();
    }, this);

    //this.text1.element.text = this.explanation;

};
2 Likes

OMG! It was that :sweat_smile:
Million thanks!