Referring to an entity array (undefined error)

Hi everyone,

I am having a problem when referring to an array of entities as it catalogues it as “undefined”, despite it is returning me its length so it exists:


        var GetColorPanel = pc.createScript('getColorPanel');
        
        GetColorPanel.attributes.add('palanca', {type: 'entity' });
        
        GetColorPanel.attributes.add('doorA', {type: 'entity', array: true, default: [""], title: "Example Entity Array"});//entity a la que queremos cambiar la textura
        
        //ExampleScript.attributes.add("entities", {type: "entity", array: true, default: [""], title: "Example Entity Array"});
        
        var angle = 0;
        
        var percentageIntensity;
        
        var initialIntensity;
        
        // initialize code called once per entity
        
        GetColorPanel.prototype.initialize = function() {
        
             angle = this.palanca.getRotation().y;
        
        };
        
        // update code called every frame
        
        GetColorPanel.prototype.update = function(dt) {
        
            var self = this;
        
            angle = this.entity.getEulerAngles().y;
        
            percentageIntensity = (angle + 90) / 180;
        
            console.log('len: ' + this.doorA.length);
        
            var i;
        
            for(i=0;i<=this.doorA.length;i++)
            {
               self.doorA[i].entity.script.receiveChangeOrder.open(percentageIntensity);
            }

I tried also in other script:

var i;

   

         for(i=0;i<=this.doorA.length;i++)
        
            {
        
                console.log("var " + i);
        
                this.doorA[i].model.meshInstances[0].material.diffuse.set(tramoRed, tramoGreen, tramoBlue);
        
                this.doorA[i].model.meshInstances[0].material.update();
        
                console.log("this is " + tramoRed + "this is " + tramoGreen + "this is " + tramoBlue);
        
            }

i found in the forum very similar topics but none of them have been of help in my case.
No mater what i change the error is always the same: “(X).doorA[i].(X) is undefined”

Thanks a lot for your help

Hi @practicas_imas,

The array contains entities, so no need to type an extra .entity when you try to access it. Do this:

self.doorA[i].script.receiveChangeOrder.open(percentageIntensity);

I see the point, but it still does not work :frowning:
I do not see what the problem could be

Not sure, try sharing a sample project so we can take a look at.

Here it is:
https://playcanvas.com/project/958250/overview/vr-gallery-22

Here’s your main issue. You have i <= this.doorA.length instead of i < this.doorA.length

Arrays are 0 indexed so you always check if it’s less than the length

2 Likes

OMG thats it! What an stupid mistake :sweat_smile:

Thank you a lot!