[SOLVED] Creating Image Element at runtime

I’m trying to generate some image elements on run time for a 2D game, and I don’t know if this is the best way to do it:

I tried cloning another element that I already have on the Scene.

  console.log('Spawning tile');
  const { tileWidth, tileHeight } = this.grid;
  const tile = this.prefab.clone();
  const localPos = this.prefab.localPosition;

  const x = 0;
  const y = 800;

  tile.setLocalPosition(new pc.Vec3(x, y, 0));
  tile.name = `tile x: ${tile.localPosition.data[0]} y: ${tile.localPosition.data[1]}`;


But it seems that the cloned element looses the screen reference and that is why I can’t see anything new on the game.

Is there a better way to do it ?

Looks like you are cloning the entity but not giving the new instance a parent?

Assuming this.prefab is a child of the right parent entity, try

const tile = this.prefab.clone();
const localPos = this.prefab.localPosition;
1 Like

That fixed the issue, thanks!