Got a nil value, but scoreText appears to be spelled correctly?

This is from the Getting Started Guide, Star Explorer. Error pointed at line 228, with scoreText having a nil value. I checked spelling, tried switching scoreText to a global value, and re-positioned scoreText, but nothing fixed the issue. I marked the line with the error using a comment in all caps and placed a huge gap right after it in hopes of helping you find it more easily. Any help please? Thanks again.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local lives = 3 local score = 0 display.setStatusBar( display.HiddenStatusBar ) local physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) --seed the random number generator math.randomseed( os.time() ) local sheetOptions = { frames = { { -- asteroid 1 x = 0, y = 0, width = 102, height = 85 }, { -- asteroid 2 x = 0, y = 85, width = 90, height = 83 }, { -- asteroid 3 x = 0, y = 168, width = 100, height = 79 }, { -- ship 4 x = 0, y = 265, width = 98, height = 79 }, { -- laser 5 x = 98, y = 265, width = 14, height = 40 }, } } local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions ) -- Initialize variables local died = false local asteroidsTable = {} local ship local gameLooopTimer local livesText local scoreText local backgroup = display.newGroup() local maingroup = display.newGroup() local uiGroup = display.newGroup() local background = display.newImageRect( backgroup, "background.png", 800, 1400 ) background.x = display.contentCenterX background.y = display.contentCenterY ship = display.newImageRect( maingroup, objectSheet, 4, 98, 79 ) ship.x = display.contentCenterX ship.y = display.contentHeight - 100 physics.addBody( ship, { radius=30, isSensor=true} ) ship.myName = "ship" local function updateText() livesText.text = "Lives: " ..lives scoreText.text = "Score: "..score end local function createAsteroid() local newAsteroid = display.newImageRect( maingroup, objectSheet, 1, 102, 85 ) table.insert( asteroidsTable, newAsteroid ) physics.addBody( newAsteroid, "dynamic", {radius=40, bounce=0.8} ) newAsteroid.myName = "asteroid" local whereFrom = math.random(3) if (whereFrom == 1) then -- left newAsteroid.x = -60 newAsteroid.y = math.random( 500 ) newAsteroid:setLinearVelocity( math.random( 40, 120 ), math.random( 20, 60 )) elseif (whereFrom == 2) then newAsteroid.x = math.random(display.contentWidth) newAsteroid.y = -60 newAsteroid:setLinearVelocity( math.random (-40, 40), math.random( 40, 120 )) else newAsteroid.x = display.contentWidth + 60 newAsteroid.y = math.random( 500 ) newAsteroid:setLinearVelocity( math.random(-120, 40), math.random( 20, 60 )) end newAsteroid:applyTorque(math.random( -6, 6 ) ) end local function FireLaser() local newLaser = display.newImageRect( maingroup, objectSheet, 5 , 14, 40 ) physics.addBody( newLaser, "dynamic", {isSensor=true} ) newLaser.isBullet = true newLaser.myName = "laser" newLaser.x = ship.x newLaser.y = ship.y newLaser:toBack() transition.to( newLaser, { y = -40, time=500, onComplete = function() display.remove( newLaser ) end} ) end ship:addEventListener( "tap", FireLaser ) local function moveShip( event ) local ship = event.target local phase = event.phase if ( "began" == phase )then -- set touch focus on the ship display.currentStage:setFocus( ship ) -- store initial offset position ship.touchOffsetX = event.x - ship.x elseif ( "moved" == phase )then --move ship to new location ship.x = event.x -ship.touchOffsetX elseif( "ended" == phase or "cancelled" == phase )then --Release touch focus on ship display.currentStage:setFocus( nil ) end -- Prevents touch propagation to underlying objects return true end ship:addEventListener("touch", moveShip) local function gameLoop() createAsteroid() for i = #asteroidsTable, 1, -1 do local thisAsteroid = asteroidsTable[i] if ( thisAsteroid.x \< -100 or thisAsteroid.x \> display.contentWidth + 100 or thisAsteroid.y \< -100 or thisAsteroid.y \> display.contentHeight + 100) then display.remove( thisAsteroid ) table.remove( asteroidsTable, i ) end end end gameLooopTimer = timer.performWithDelay( math.random( 250, 1000 ), gameLoop, 0 ) local function restoreShip() ship.isBodyActive = false ship.x = display.contentCenterX ship.y = display.contentHeight - 100 -- Fade in ship transition.to( ship, {alpha=1, time=4000, onComplete = function() ship.isBodyActive = true died = false end } ) end local function onCollision ( event ) if event.phase == "began" then local obj1 = event.object1 local obj2 = event.object2 if ( (obj1.myName == "laser" and obj2.myName == "asteroid" or obj1.myName == "asteroid" and obj2.myName == "laser") )then display.remove( obj1 ) display.remove( obj2 ) for i = #asteroidsTable, 1, -1 do if ( asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2 )then table.remove(asteroidsTable, i) break end end score = score + 100 scoreText.text = "Score: " .. score -- ERROR WAS HERE elseif ( ( obj1.myName == "asteroid" and obj2.myName == "ship" or obj1.myName == "ship" and obj2.myName == "asteroid") )then if died == false then died = true lives = lives - 1 livesText = "Lives: " .. lives if lives == 0 then display.remove( ship ) else ship.alpha = 0 timer.performWithDelay( 1000, restoreShip ) end end end end end Runtime:addEventListener( "collision", onCollision )

 

When you post code, you can add “starting line number” to your post. That way it is easier for others to see what line is causing the issues.

In your case, the problem is that you haven’t actually yet created a text display object and the game crashes because it is trying to give set  scoreText.text some value, but since  scoreText is not a display object (i.e. it is not a Lua table), the .text entry can’t exist. In other words, you need to first create the text object and then you can update the text via  scoreText.text.

See documentation: https://docs.coronalabs.com/api/library/display/newText.html.

Sorry it took so long to get back. Yes, that definitely makes sense, though I’m surprised I would miss something that obvious in the guide. Will get back once I fix the code.

When you post code, you can add “starting line number” to your post. That way it is easier for others to see what line is causing the issues.

In your case, the problem is that you haven’t actually yet created a text display object and the game crashes because it is trying to give set  scoreText.text some value, but since  scoreText is not a display object (i.e. it is not a Lua table), the .text entry can’t exist. In other words, you need to first create the text object and then you can update the text via  scoreText.text.

See documentation: https://docs.coronalabs.com/api/library/display/newText.html.

Sorry it took so long to get back. Yes, that definitely makes sense, though I’m surprised I would miss something that obvious in the guide. Will get back once I fix the code.