Random generation

How would I make the following code respawn an already existing entity (that is disabled) and reenable the clone while spawning in the random area between 1000 1000?
This is my demo code.

var EntityCreator = pc.createScript('entityCreator');

EntityCreator.attributes.add('material', {
    type: 'asset',
    assetType: 'material'

EntityCreator.attributes.add('boxDimensions', {
    type: 'number',
    default: 10

EntityCreator.attributes.add('lifetime', {
    type: 'number',
    default: 5

EntityCreator.attributes.add('maxCubes', {
    type: 'number',
    default: 10

// initialize code called once per entity
EntityCreator.prototype.initialize = function() {
    this.entities = [];

// update code called every frame
EntityCreator.prototype.update = function(dt) {
    // Spawn new cubes if there are less than maxCubes
    while (this.entities.length < this.maxCubes) {

    // Loop through Entities and delete them when their time is up
    for (i = 0; i < this.entities.length; i++) {
        this.entities[i].timer -= dt;
        if (this.entities[i].timer < 0) {
            // entity.destroy() deletes all components and removes Entity from the hierarchy

            // Remove from the local list
            this.entities.splice(i, 1);

EntityCreator.prototype.spawnCube = function () {
    var entity = new pc.Entity();

    // Add a new Model Component and add it to the Entity.
    entity.addComponent("render", {
        type: 'box'
    // set material
    entity.render.material = this.material.resource;

    // Move to a random position
        pc.math.random(-this.boxDimensions, this.boxDimensions),
        pc.math.random(-this.boxDimensions, this.boxDimensions),
        pc.math.random(-this.boxDimensions, this.boxDimensions)

    // Add to the Hierarchy

    // Store in a list for some random duration before deleting
        entity: entity,
        timer: pc.math.random(0, this.lifetime)

Hi @Jacob_Mcbride,

This tutorial does that exactly:


1 Like

Thank you @leonidas, you have helped alot of times.

1 Like

wait, I need 3 things to be randomly generated, and not on tiles but on the ground, can you modify the script to do so?

Sorry I can’t do that, you will have to study the code and learn to do it yourself.

1 Like

I cannot.

I barely know how to code.