I'm having trouble accessing a property of an object I store in an array.
I store the properties like so:
let lights = [];
let newlightinfo = {
active: true, // default value
directional: false, // default value
x: 0, // default value
y: 1, // default value
z: 0, // default value
ia: vec3(75, 75, 75), // default value
id: vec3(175, 175, 175), // default value
is: vec3(RGB, RGB, RGB) // default value
}
let addlightbutton = {
add: function() {
if (lights.length <= MAX_LIGHTS) {
let lightinfo = {
active: newlightinfo.active,
directional: newlightinfo.directional,
x: newlightinfo.x,
y: newlightinfo.y,
z: newlightinfo.z,
ia: newlightinfo.ia,
id: newlightinfo.id,
is: newlightinfo.is
}
lights.push(lightinfo);
}
}
I also tried to simply do let lightinfo = newlightinfo
, but it also didn't work, which makes sense.
I'm trying to do:
function lightInfo()
{
if(lights.length > 0) {
console.log(JSON.stringify(lights[0]));
console.log(JSON.stringify(lights[0].x));
// Light information
for(let i = 0; i < MAX_LIGHTS; i++) {
let x = lights[i].x; // error line
...
But I'm getting an error - Uncaught TypeError: Cannot read properties of undefined (reading 'x').
I can't understand why since when printing out lights[0]
and lights[0].x
right before accessing my array everything seems right.
Any ideas on why this is happening?