[SOLVED]Sprite sizing problem: Cannot read property 'setSize' of null

I’m trying to set sprite hitboxes to match the size of the graphics. I currently have this:
image
As you can see, the hitbox size doesn’t match.
I know that you have to set the hitbox size separately using setSize() and setOffset(), so I’m using this here in my code:

create(){
  this.coordinates = {
    //key: [x,y,scale]
    0:[20, 20, 0.5],
    1:[60, 60, 0.8],
    2:[100, 100, 1],
    3:[140, 140, 1.2],
    4:[180, 180, 0.3],
    5:[220, 220, 2],
    6:[260, 260, 0.7],
    7:[300, 300, 1.7],
  }
  this.astros = this.physics.add.group();
  this.scatterObjects(this.coordinates);
}

scatterObjects(coords){
    let allFrames = this.anims.generateFrameNumbers('junk');
    allFrames.forEach(element => {
      console.log(element.frame);
      console.log(coords[element.frame][0]);
      let thing = new Astro(this,coords[element.frame][0],coords[element.frame][1],'junk',element.frame)
      thing.setScale(coords[element.frame][2]);
      console.log(thing);
      //This line causes issues:
      // thing.setSize(45*coords[element.frame][2], 60*coords[element.frame][2]);
      this.astros.add(thing);
    });

I get the following error:
image
I don’t really understand how that can happen, since the object I’m calling this setSize()function on is printed right above and it is certainly not null…

Any help would be apperciated :slight_smile:

Ah! I realised that no physics were assigned to the sprite objects, so I moved this line

this.astros.add(thing);

before

thing.setSize(45*coords[element.frame][2], 60*coords[element.frame][2]);

and that seems to have done the trick, though the sizing needs some tweaking

This line was sufficient to set the hitboxes to the right size.

thing.setSize(45, 60);

image