Group.children[i] is undefined

I am trying to loop through a group of sprites, but when i try to log every specific child of that group with:

for(let i = 0; i < invaders.children.length; i++){
    console.log(invaders.children[i].sprite)
}

It logs “undefined”.
I have logged the entire invaders group elsewhere in the code and then it logs everything correctly.

Maybe variable is not defined in the scope where this code runs. If it’s defined in the same scope invaders.children[i] should not be undefined.

I don’t think sprite property exist on children’s of group. so invaders.children[i].sprite would return undefined.

var invaders is a global variable, so that shouldn’t be a problem.

I have this piece of code in another function, that works without any issues:

for (let i = 0; i < invaders.children.length; i++) {
    if (invaders.children[i].alive) {
        if (invaders.children[i].x <= 40) {
            //Some code
        }
    }
}

And when i try to log “invaders.children[i]” it returns “object Object

Inspect these objects. If they are sprites, I don’t think they have a member ‘sprite’. This would explain that when you try to display this member, the value returned is undefined.

Something is very wrong here. I can’t even log “console.log(invaders)” in this function…
It just returns [object Object]

invaders is a group. When you log it, it’s showed as an object, nothing special here.

Except when i log it else where it returns the entire group with all its properties.

alive: true
alpha: 1
cameraOffset: Phaser.Point {x: 0, y: 0, type: 25}
children: (55) [P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, 
P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, 
 P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, 
P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, 
P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, 
 P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, 
P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite, P…r.Sprite]
classType: ƒ (game, x, y, key, frame)
cursor: Phaser.Sprite {type: 0, physicsType: 0, position: P…r.Point, scale: P…r.Point, pivot: P… 
r.Point, …}
cursorIndex: 0
enableBody: true
enableBodyDebug: false
exists: true
filterArea: null
fixedToCamera: false
game: Phaser.Game {id: 0, config: {…}, physicsConfig: undefined, parent: "spaceinvaders", width: 720, …}
hash: []
hitArea: null
ignoreChildInput: false
ignoreDestroy: false
inputEnableChildren: false
name: "group"
onChildInputDown: Phaser.Signal {}
onChildInputOut: Phaser.Signal {}
onChildInputOver: Phaser.Signal {}
onChildInputUp: Phaser.Signal {}
onDestroy: Phaser.Signal {}
parent: Phaser.World {game: P…r.Game, name: "__world", z: 0, position: P…r.Point, scale: P…    r.Point, …}
pendingDestroy: false
physicsBodyType: 1
physicsSortDirection: null
physicsType: 7
pivot: Phaser.Point {x: 0, y: 0, type: 25}
position: Phaser.Point {x: 0, y: 0, type: 25}
renderable: false
rotation: 0
scale: Phaser.Point {x: 1, y: 1, type: 25}
type: 7
updateOnlyExistingChildren: false
visible: true
worldAlpha: 1
worldPosition: Phaser.Point {x: 0, y: 0, type: 25}
worldRotation: -0
worldScale: Phaser.Point {x: 1, y: 1, type: 25}
worldTransform: Phaser.Matrix {a: 1, b: 0, c: 0, d: 1, tx: 0, …}
z: 4
_bounds: Phaser.Rectangle {x: 0, y: 0, width: 0, height: 0, type: 22}
_cacheAsBitmap: false
_cacheIsDirty: false
_cr: 1
_currentBounds: null
_mask: null
_sortProperty: "z"
_sr: 0
angle: (...)
bottom: (...)
cacheAsBitmap: (...)
centerX: (...)
centerY: (...)
filters: (...)
height: (...)
left: (...)
length: (...)
mask: (...)
right: (...)
top: (...)
total: (...)
width: (...)
worldVisible: (...)
x: (...)
y: (...)
__proto__: PIXI.DisplayObjectContainer

What’s the command used to display this ?

simply console.log(invaders)

But i’m a dum dum. I all this time i typed console.log(invaders.children[i] + " some string")
I didn’t realize that having a string as well would mess everything up. If i only type console.log(invaders.children[i]) it works.