Scene create doesn't create an object again (i think)

I have some objects (2 sprites and some texts) that were created in the composer.newScene(), i declare it’s values(display.etc) in the scene:create(event) and i change it’s colors with setFillColor in a colision function declared in the composer:newScene, no problem here, when the objects make collision they change it’s colors normally,but when i exit the scene, return to the menu and enter again the scene, to replay the game, when it makes colision brings error: attempt to call method setFillColor a nil value, wich implies that the objects doesn’t exists, but they do, i think, the sprites appear in the screen but when the collision function tries to change it’s colors that’s all. It’s make me crazy so please help me figure it out.

The colision code look like this, and the objects like this in the scene = composer.newScene() and scene:create(event)

Colision Function

if (event.phase == "began") then
    
    local obj1 = event.object1
    local obj2 = event.object2

if ( obj2.myName == "blueP" and obj1.myName == "white")     then  
       splash:setFillColor( 0.2, 0.16, 1 ) 
       splash.x = obj2.x
       splash.y = obj2.y
       splash:play()
       display.remove(obj2)
       ball:setFillColor( 0.2, 0.16, 1 ) 
       text:setFillColor( 0.2, 0.16, 1 )
       count:setFillColor( 0.2, 0.16, 1 )
       score:setFillColor( 0.2, 0.16, 1 )
       pointsTxt:setFillColor( 0.2, 0.16, 1 )
       clockText:setFillColor( 0.2, 0.16, 1 )
       bonus:setFillColor( 0.2, 0.16, 1 )
       audio.play(blipSound)

scene = composer.newScene()

local splash
local ball 

scene:create(event)

splash = display.newSprite(mainGroup,sheetSplash,animSplash)
 ball = display.newSprite(mainGroup,sheetBall,animBounce)

I repeat, it work fine the first time the scene it’s created, but when i change the scene and enter again doesn’t work, and i destroy the scene in scene:hide, phase == “did” with composer.removeScene

Hi!
I suspect there’s something going on with proper object and physics body removal.
Allow me to elaborate on a few things…

First, to force composer to reload scenes you can set the following value:

composer.recycleOnSceneChange = true

Second, make sure your objects (or their parent group) are inserted in the scene’s sceneGroup;
if they are inserted then they should be removed properly by composer when switching scenes.

Third, if you’re still having issues after applying the aforementioned then try to manually remove all existing object’s physics body before switching composer scenes, probably inside scene:hide().

Hope that helps!

I tried everything and it’s the same, the colision doesn’t recognize any object, and only one have physic body , the ball, the others are just texts and sprite, something strange it’s that i tried to put the splash object to be created in the collision function, so it’s sure that it exist every time there’s a colission, but now brings this error: bad argument # -1 to “newSprite” (Proxy expected got nil), that i assume it’s the same situation, looks like the splash object it’s never created, exactly the part of local splash, because the error it’s in that exact line.

local function colision(event) 
  
  if (event.phase == "began") then
    
    local obj1 = event.object1
    local obj2 = event.object2

   local obj3 = text   -- I tried this, which i think should work, but im not sure i referenced the object correctly

local splash = display.newSprite(mainGroup,sheetSplash,animSplash)     --Here brings the error
    
     if ( obj2.myName == "blueP" and obj1.myName == "white")     then  
       splash:setFillColor( 0.2, 0.16, 1 ) 
       splash.x = obj2.x
       splash.y = obj2.y
       splash:play()
       display.remove(obj2)
       obj1:setFillColor( 0.2, 0.16, 1 )                --Modify the ball object via it's reference in the function
       obj3:setFillColor( 0.2, 0.16, 1 )
       count:setFillColor( 0.2, 0.16, 1 )
       score:setFillColor( 0.2, 0.16, 1 )
       pointsTxt:setFillColor( 0.2, 0.16, 1 )
       clockText:setFillColor( 0.2, 0.16, 1 )
       bonus:setFillColor( 0.2, 0.16, 1 )
       audio.play(blipSound)

The ball object, that in the function it’s obj1 looks to work with that change, so i tried to pass the other objects (texts) to the function to be referenced via new objects but didn’t work, or maybe i do it incorrectly.
And yes, i put all the objects in display groups and i add those display groups to the sceneGroup

Do you remove global collision listener properly?
See Solar2D Documentation — Developer Guides | Events/Listeners

Maybe use use local collision listener:-)
Have a nice day:-)

Oh my god that work, i really thank you, but it’s confusing because i did that once but didn’t work, maybe i writed wrong, i basically just tried to copy the structure of the tutorial and i remember remove that listener, anyway thank you man you save my life.