[SOLVED] Script to turn on and off

I’m looking to get my script to turn something on after a set time and then turn it off… I can get it to turn on but I can’t get it to turn off … any help would be greatly appreciated. Here is my script:

var TimerGraphs = pc.createScript('timergraphs');

TimerGraphs.attributes.add("graph", { type: "entity", title: "Graph" });


// initialize code called once per entity
TimerGraphs.prototype.initialize = function() {
    this.timer = 0;
    this.isTimerActive = true;
};

// Begin timer by calling this function
TimerGraphs.prototype.beginTimer = function(){
    this.isTimerActive = true;
};

// update code called every frame
TimerGraphs.prototype.update = function(dt) {
   if(!this.isTimerActive)
        return;

    this.timer += dt;

    if (this.timer > 1) {
 

       this.graph.enabled = !this.graph.enabled;
       this.timer = 0;
       this.isTimerActive = false;



    if (this.timer > 5) {
 

       this.graph.enabled = !this.graph.enabled;

       
      this.timer = 0;
        this.isTimerActive = false;

    }


  
    
    }


};



        

Hey, so, for now, the script will work for only the first if condition and it will never go to the second if state i.e if (this.timer > 5) because this.timer is initialized to 0 in the first condition every time. You will have to add another variable which checks first condition 1 time and then check second condition when first is fulfilled.

Assuming, you are enabling the graph entity in first condition and disabling it in second condition, here is how you can achieve it.

TimerGraphs.prototype.initialize = function() {
    this.timer = 0;
    this.isTimerActive = true;
   this.isCheckTimer1Condition = true;
};
TimerGraphs.prototype.update = function(dt) {
   if(!this.isTimerActive)
        return;

    this.timer += dt;

    if (this.timer > 1 && this.isCheckTimer1Condition) {
 

       this.graph.enabled = !this.graph.enabled;
       this.timer = 0;
       this.isTimerActive = false;
       this.isCheckTimer1Condition = false;

}

   else if (this.timer > 5 && !this.isCheckTimer1Condition) {
 

       this.graph.enabled = !this.graph.enabled;

       
      this.timer = 0;
        this.isTimerActive = false;
this.isCheckTimer1Condition = true;

    }
};
2 Likes

@saif As far I can see both statements do the same a this moment, so there is probably another reason why the entity can’t be disabled.

@davidj2022 Are you be able to share a link of your project or is it a private project?

2 Likes

i felt like that should work… but sadly it doesn’t turn it back off.

It is private sadly… the script is intended to just turn a 2d image on and off from a 2d screen

What happens if you enable the entity in the editor and disable the entity in the initialize function?

seems to react the same way sadly

If you add me to the project I can take a look.

our you able to access this? many thanks

test | Editor (playcanvas.com)

For some reason it’s working now?

Have looked into your test project. It only works first time because you are not setting this.isTimerActive to true again. Either remove this condition

if(!this.isTimerActive)
        return;

or add the logic to enable the isTimerActive Boolean

2 Likes

Brilliant! thank you both … super kind… now works :slight_smile:

1 Like