Get current color

I changed the material of a template at runtime, but when I try to get the new color, it is returning me as if I had not changed it.

Action.prototype.createBalls = function() {
    clampBall   = pc.app.assets.get("44923196");
    balls       = pc.app.root.findByName("balls");
    var x       = this.columnsIni;
    var y       = this.rowIni;
    
    for(var i =0; i<this.rows; i++){
        for(var a=0; a<this.columns; a++){
            
            //Initial position
            if(a===0){
                
                if(i % 2===0){
                    x = this.columnsIni;
                }else{
                    x = this.columnsIni + this.ballEspaceAjust;
                }
                
                
            }
            
            //new instance
            if((i % 2===0) || ( a <(this.columns-1))){

                instance  = clampBall.resource.instantiate(); 
                balls.addChild(instance);
                instance.setPosition(x,y,0);
                //instance.name = instance.name + '_' + i + '_' + a;
                
                color = Math.floor(Math.random() * this.colors.length);
                instance.model.meshInstances[0].material = this.colors[color].resource;
            }
            
            x = x + this.ballEspace;
        }
        
         y = y - (this.ballEspace-0.06);
    }
    
    //position of game
    var xGameTotal   = (this.ballEspace * this.columns)-this.ballEspace;
    var gamePosition = (xGameTotal/2);
    balls.setPosition(-gamePosition,0,0);
    
};

helllpp me

I think your color value is not picked correctly. If you want a random from array, you can use:

var index = Math.floor(pc.math.random(0, this.colors.length));
instance.model.meshInstances[0].material = this.colors[index].resource;
1 Like

it didn’t work, I did something provisional, until I saw what I can do. thankssssss

one cheers for attributes \ o /