[SOLVED] How to make entity disappear and showup again?

Hi, I’m trying to do EAT coin + coin regeneration.
my way to make coin disappear is to use this.entity.destroy().
But I don’t know how to make it showup like regeneration.

Is there any solution or function to solve it ? Or using destroy() to make it disappear is the wrong way ? Any tips or sample project thanks.

Hello @11144! If it is the same entity I just would use this.entity.enabled = false; and this.entity.enabled = true;.

@Albertos
I got some issue so more question please.
if I using this.entity.enabled = false; to make it disappear
and after that using this.entity.enable = true; to make it enable.
Will initialize function run again after enabled ?

No, if an entity or script is disabled when the application starts the initialize method is called only the first time the entity is enabled.

@Albertos
I found this.entity.enabled = true; will not make my coin appear.
So what I had think about is… I have many coin entity named Coin (all same) and all Coin entity has same script named coin.js
will that be the problem to control entity to enable = true/false ?

Sorry for private project… Or maybe I can make a sample to test it and linked it to here.

@11144, this.entity.enabled = true; will make that entity enabled. If you have implemented any functionality in initialize and it’s not working on enabling again then you can use onEnable functionality of script to perform particular functions there:

Rotate.prototype.initialize = function () {
    this.on("enable", function () {
        this.startingFunctionalities();
    });
    this.startingFunctionalities();
    this.on("disable", function () {
   this.entity.enabled = false;// etc
    });
};

Will this.entity.enabled = true; call this.on(“enable”, function() { ?
Or you are just giving me an example so I need to call “enable” using app.fire like ?

this.entity.enabled = true; calls the enable function of that script. If that script is enabled for the very first time then only initialize function will be called and enable function will not called. And after that, on every time, when the entity is enabled, only it’s onEnable function will be called.

@saif
I have checked disable working fine. But after this.entity.enabled = false;
this.entity.enabled = true; is not working… So… Here is my code.

ps : I have tried some codes. And I found this.entity.enabled = true; work fine inside onCollision function. But not working inside .update function.

var Coin = pc.createScript('coin');
var regen = false;
var timer = 0;

// initialize code called once per entity
Coin.prototype.initialize = function() {
    var app = this.app;
    this.on('enable', function(){
        console.log("ENTITY ENABLED !!");
    }, this);
    
    this.on('disable', function(){
        console.log("ENTITY DISABLED !!");
    }, this);
    
    var playerInven = app.root.findByName('Player');
    this.pi = playerInven.script.playerInventory;
    this.entity.collision.on('collisionstart',this.onCollision,this);
};


Coin.prototype.onCollision = function (result) {
    if(result.other.name ==='Player'){
        this.pi.GetCoin(2);
        this.entity.enabled = false;
        regen = true;
    }
};

Coin.prototype.update = function (dt) {
    if(regen) {
        this.entity.enabled = true;
        regen = false;
    }
};
// update code called every frame

Hi, @11144,I think it’s because the update function is only running when the entity is enabled.you can learn more about the lifecycle event here:https://developer.playcanvas.com/en/user-manual/scripting/anatomy/

2 Likes

@FBplus
I have test several codes.
I found update is still running with disable.

But when I disable it once, this.entity is no longer exist.
So that’s the reason why this.entity.enabled = true; doesn’t work.

For Solution, I copied guid to global, and restore it inside update function and it is doing well.

Your “scope” and “lifecycle” mention was good. Thanks.

1 Like

I think I misunderstood you at the begin. Possible you can first clone a new coin entity when the player eat a coin and after that destroy the coin?

Problem solved. Thank you. @Albertos

But I got another question.
If I Duplicate an entity in HIERARCHY. Will GUID be same with original one?
Because I found guid is duplicating between two entity (same Naming) but some other same naming was not.

So what is your solution?

New question new topic. :yum:

Well my solution is below. And I think this will be helpful for everyone.

when I disable it once, this.entity is no longer exist.
So that’s the reason why this.entity.enabled = true; doesn’t work.
For Solution, I copied guid to global, and restore it inside update function and it is doing well.

1 Like

No. The GUID should be different.

1 Like