Attempt to call method '' (a nil value) when reentering scene

I have a simple storyboard setup.
Main to menu to either levelSelection or options. levelSelection to level1,2,3, etc.

I get no errors if I do menu - levelSelection - level1.
When I go menu - levelSelection - level1 - levelSelection - level1 I get the "Attempt to call method ‘(somemethod)’ (a nil value)… "
I get the same error if I go menu - options - menu - levelSelection - level1

The method in question is “getLinearVelocity()” in cube:touch

function helperFunctions.createCube( size, x, y )     -- Create cube     local cube = display.newRect( x, y, size, size )     cube:setFillColor( 178 )     cube.isFixedRotation = true     local density = 2025 / (size\*size)     physics.addBody( cube, { density=density, friction=0, bounce=0 } )          -- Touch method     function cube:touch( event )         local vx, vy = self:getLinearVelocity()         if( vx == 0 and vy == 0) then             if event.phase == "began" then                 -- blah blah             end             return true         end     end              cube:addEventListener( "touch", cube )     return cube end

in my level1.lua at the top I do: local helperFunctions = require( “helperFunctions” )
and in scene:createScene: cube = helperFunctions.createCube( 45, 15, 15 )

I found metatables to be connected with this sort of error when I researched it.
Is that something I have to implement?

can you post the whole error backtrace?

I assume it’s this?

35lgklw.jpg

Am I allowed to bump my topic?
If not, I apologize.

I never got a reply so I was afraid my topic had slipped through.

Yes, you can bump, we just ask that you wait at least a day or two.

That is a good start to pointing out the problem.  What is line 128 of your helperFunctions.lua file?

Most likely one of two things is causing this. 

  1. you have overwritten the object that you’re trying to get the linear velocity of.  This includes having removed the object before trying to get the velocity.

  2. It’s not a physics body.

Line 128 is:

local vx, vy = self:getLinearVelocity()

It’s in the touch function.

I have 2 buttons for when I finish the level that remove it.
Retry that goes to a temporary scene, removes level1 and sends me back to it.
Exit that sends me to the levelSelection. In the enterScene section I do storyboard.removeAll().
Am I not removing the cube and restarting the level correctly?

Every time a cube is created it should do physics.addBody().
Is there a way to check if it isn’t?

Also, what I find weird is the error happening if I go to options as well, a scene that has nothing do to with that function.
 

Main - Menu - going to Options and back - levelSelection - level1.
The first time I load the helperFunctions module is in level1.
Why does the error happen in this case?

You can turn on physics debug mode and it will show an overlay over each physics object.  If your box doesn’t have an overlay, it’s not physics.

Either that or self is likely nil.

It’s a physics problem after all.
physics.stop() happens and doesn’t restart after.

I must have messed up somewhere in the scene transition.
Thank you for the help  :slight_smile:


I still can’t figure out the what stops the physics engine.
Going back to any scene other than level1, doesn’t let physics start.

can you post the whole error backtrace?

I assume it’s this?

35lgklw.jpg

Am I allowed to bump my topic?
If not, I apologize.

I never got a reply so I was afraid my topic had slipped through.

Yes, you can bump, we just ask that you wait at least a day or two.

That is a good start to pointing out the problem.  What is line 128 of your helperFunctions.lua file?

Most likely one of two things is causing this. 

  1. you have overwritten the object that you’re trying to get the linear velocity of.  This includes having removed the object before trying to get the velocity.

  2. It’s not a physics body.

Line 128 is:

local vx, vy = self:getLinearVelocity()

It’s in the touch function.

I have 2 buttons for when I finish the level that remove it.
Retry that goes to a temporary scene, removes level1 and sends me back to it.
Exit that sends me to the levelSelection. In the enterScene section I do storyboard.removeAll().
Am I not removing the cube and restarting the level correctly?

Every time a cube is created it should do physics.addBody().
Is there a way to check if it isn’t?

Also, what I find weird is the error happening if I go to options as well, a scene that has nothing do to with that function.
 

Main - Menu - going to Options and back - levelSelection - level1.
The first time I load the helperFunctions module is in level1.
Why does the error happen in this case?

You can turn on physics debug mode and it will show an overlay over each physics object.  If your box doesn’t have an overlay, it’s not physics.

Either that or self is likely nil.

It’s a physics problem after all.
physics.stop() happens and doesn’t restart after.

I must have messed up somewhere in the scene transition.
Thank you for the help  :slight_smile:


I still can’t figure out the what stops the physics engine.
Going back to any scene other than level1, doesn’t let physics start.