Getting 'Undefined' Despite Entity Existing In Hierarchy

Hi everyone,

In our new game, we are trying to clone children of a certain entity. The code is as follows.

/* jshint esversion: 6*/

var CloneFallingObjects = pc.createScript('cloneFallingObjects');

// initialize code called once per entity
CloneFallingObjects.prototype.initialize = function() {
    
    let object_array = ["GreenObject", "RedObject", "BlueObject"];
    let position_array = [(-2.017, 7.573, 0.035), (-0.318, 7.594, 0.035), (1.404, 7.615, 0.111)];
    
    setTimeout(function(){ 
         
        let newEntity = new pc.Entity();
        
        let number = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
        let number2 = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
        
        newEntity = this.entity.findByName(object_array[(number - 1)].toString());
        
        newEntity.setLocalPosition(position_array[(number2 -1)]);
        
    }, 1000);
    
    
};

// update code called every frame
CloneFallingObjects.prototype.update = function(dt) {

};

However, we get the below error.

This is despite the entity clearly being in the hierarchy. The project can be found at https://playcanvas.com/editor/scene/870258.

How can we proceed?

Inside your setTimeout method this doesn’t refer to the script but to the anonymous function running. You need to change the context at which you running in there, doing something like this:

    setTimeout(function(){ 

       // your code
        
    }.bind(this), 1000);
1 Like

Thanks, the error is gone now.

@Leonidas, the falling objects still do not fall, despite adding a line to enable newEntity. Why could this be?

Can you explain a bit what you are trying to achieve?

I need the objects, selected randomly, to keep falling one at a time onto the rotating bar.

So you are saying you are cloning object but from what I see you only create a new blank entity each time, no model or anything to actually have it rendered.

To clone an entity you need to use the clone method and then add it to the hierarchy using the addChild method. Check the following tutorials on how this works:

https://developer.playcanvas.com/en/tutorials/?tags=procedural

I have changed the script now. Please let me know if this is better.


var CloneFallingObjects = pc.createScript('cloneFallingObjects');

// initialize code called once per entity
CloneFallingObjects.prototype.initialize = function() {
    
    let object_array = ["GreenObject", "RedObject", "BlueObject"];
    let position_array = [(-2.017, 7.573, 0.035), (-0.318, 7.594, 0.035), (1.404, 7.615, 0.111)];
    
    setTimeout(function(){ 
         
        let newEntity = new pc.Entity();
        
        let number = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
        let number2 = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
        
        newEntity = this.entity.findByName(object_array[(number - 1)].toString()).clone();
        
        this.app.root.addChild(newEntity);
        
        newEntity.setLocalPosition(position_array[(number2 -1)]);
        
        newEntity.enabled = true;
        
    }.bind(this), 1000);
    
    
};

// update code called every frame
CloneFallingObjects.prototype.update = function(dt) {

};


// swap method called for script hot-reloading
// inherit your script state here
// CloneFallingObjects.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Because, despite using these methods, it doesn’t work.

This line isn’t required, the clone method creates a new pc.Entity.

It seems better, if it’s not working you have to debug each line to check if everything is running with the expected result.

1 Like

OK, I’ll remove the line, and debug. Thanks.

1 Like