Keeping function nil when object isn't there?

So when my game starts my eventListener starts and i need to tap the screen. But if i tap the screen before the object spawns then i get a nil error. How would i check if the object is there or not? Here’s original code and then what i tried to do.

Original –

 local object = {} local objectRemove = 1 local function touchy(event) local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end end end Runtime:addEventListener( "touch", touchy )

What I tried – 

 local object = {} local objectRemove = 1 local function touchy(event) if object ~= nil then else local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end end end Runtime:addEventListener( "touch", touchy )

Thanks!

To me it looks like you simply have your code on the wrong side of the if-else statement:

local object = {} local objectRemove = 1 local function touchy(event) if object ~= nil then local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end else end end Runtime:addEventListener( "touch", touchy )

Or a little more human readable:

local object = {} local objectRemove = 1 local function touchy(event) if object then local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end end end Runtime:addEventListener( "touch", touchy )

Alright thanks! I had to do something a bit different tho.

 local object = {} local objectRemove = 1 local objectStartCheck = 1 local function touchy(event) if object[objectStartCheck] then local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end end end Runtime:addEventListener( "touch", touchy )

I just added another counter for the beginning.

–SonicX278 

To me it looks like you simply have your code on the wrong side of the if-else statement:

local object = {} local objectRemove = 1 local function touchy(event) if object ~= nil then local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end else end end Runtime:addEventListener( "touch", touchy )

Or a little more human readable:

local object = {} local objectRemove = 1 local function touchy(event) if object then local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end end end Runtime:addEventListener( "touch", touchy )

Alright thanks! I had to do something a bit different tho.

 local object = {} local objectRemove = 1 local objectStartCheck = 1 local function touchy(event) if object[objectStartCheck] then local objectWidth = object[objectCheck].width if event.phase == "began" then display.remove( object[objectRemove] ) objectRemove = objectRemove + 1 else composer.gotoScene("gameOver") end end end end Runtime:addEventListener( "touch", touchy )

I just added another counter for the beginning.

–SonicX278