Timer not affecting an object after leaving and returning to a Scene.

Hi folks,

            I have a scene with a button, when I tap the button it starts a timer (6 seconds).
Upon tap it sets an image.isVisible = true.
Upon timer completion it runs a function in that same scene which turns the image.isVisible = false again + a print statement.

Now that works fine.

But if I leave the scene and then go back, it activates: prints statement but then doesn’t change the .isVisible.

I believe this is because it’s not ‘technically’ the same ‘image’.

local Unbuildable = display.newImageRect("Images/UnBuildable.png", 150, 100); ...etc

Is there a way to get the timer to do the .isVisible change to the new ‘Unbuildable’ object which has the same name?
Or does the fact I change scene and create a new ‘Unbuildable’ mean I can’t do this.

Any Idea’s would be greatly appreciated.

*Edited to clarify the Question*

Do you have this image creation in enterScene? That would make a new image every time you open the scene. Should (probably) be in createScene. Any help in that?

I don’t seem to have an obvious enterScene & createScene, they might be in Storyboard or something?
I use the Director Class (Haha, I hope that’s not outdated or anything.)

My .lua file basically looks like this (Shortened):

module(..., package.seeall); function new() local localGroup = display.newGroup(); --CONTENT CREATION SECTION-- local UnBuildable= display.newImageRect("Images/UnBuildable.png", 150, 100); UnBuildable:setReferencePoint(display.TopLeftReferencePoint); UnBuildable.x = 460; UnBuildable.y = 188; if unitData:retrieve("TrainingProgress") == 1 then --Show image or not? UnBuildable.isVisible = true; else UnBuildable.isVisible = false; end --BUTTON PRESS AND TIMER FUNCTIONS-- local function unitBuilt() -- Makes function for completion of timer. print(Data[unitProgressData].name .. " Unit Trained") --print check unitData:store("TrainingProgress", 0); --Allows reuse of button. unitData:save() UnBuildable.isVisible = false; -- timer completed, changes visibility off. end local function unitBuild(e) -- function for pressing of button to begin timer. if(e.phase == "ended" and (unitData:retrieve("TrainingProgress")) == 0) then UnBuildable.isVisible = true;--turn on image visibility timer.performWithDelay( 6000, unitBuilt, 1 ) --6 second timer unitData:store("TrainingProgress", 1); -- Stops reuse of button. unitData:save() end end buttonBuild:addEventListener("touch", unitBuild); return localGroup; end

So, when I leave and come back, it’ll create the image again it all again. 
So in the unitBuilt function, UnBuildable.isVisible doesn’t seem to find the new UnBuildable that was re-made and associate with it.

Does that help?

Oh right, I was referring to Storyboard, sorry I have no experience with Director. Storyboard is the officially supported solution, big fan myself, but many seem to use Director still.

*Edited to clarify the Question*

Do you have this image creation in enterScene? That would make a new image every time you open the scene. Should (probably) be in createScene. Any help in that?

I don’t seem to have an obvious enterScene & createScene, they might be in Storyboard or something?
I use the Director Class (Haha, I hope that’s not outdated or anything.)

My .lua file basically looks like this (Shortened):

module(..., package.seeall); function new() local localGroup = display.newGroup(); --CONTENT CREATION SECTION-- local UnBuildable= display.newImageRect("Images/UnBuildable.png", 150, 100); UnBuildable:setReferencePoint(display.TopLeftReferencePoint); UnBuildable.x = 460; UnBuildable.y = 188; if unitData:retrieve("TrainingProgress") == 1 then --Show image or not? UnBuildable.isVisible = true; else UnBuildable.isVisible = false; end --BUTTON PRESS AND TIMER FUNCTIONS-- local function unitBuilt() -- Makes function for completion of timer. print(Data[unitProgressData].name .. " Unit Trained") --print check unitData:store("TrainingProgress", 0); --Allows reuse of button. unitData:save() UnBuildable.isVisible = false; -- timer completed, changes visibility off. end local function unitBuild(e) -- function for pressing of button to begin timer. if(e.phase == "ended" and (unitData:retrieve("TrainingProgress")) == 0) then UnBuildable.isVisible = true;--turn on image visibility timer.performWithDelay( 6000, unitBuilt, 1 ) --6 second timer unitData:store("TrainingProgress", 1); -- Stops reuse of button. unitData:save() end end buttonBuild:addEventListener("touch", unitBuild); return localGroup; end

So, when I leave and come back, it’ll create the image again it all again. 
So in the unitBuilt function, UnBuildable.isVisible doesn’t seem to find the new UnBuildable that was re-made and associate with it.

Does that help?

Oh right, I was referring to Storyboard, sorry I have no experience with Director. Storyboard is the officially supported solution, big fan myself, but many seem to use Director still.