Attempt to index local 'hex' (a nil value)

I am trying to choose randomobject (color) from table and then applying it to newrect but i am getting the follwoing error

Attempt to index local ‘hex’ (a nil value)

File: main.lua

Line: 17

stack traceback:

main.lua:17: in function ‘convHexColor’

main.lua:40: in function ‘_listener’

?: in function <?:167>

?: in function <?:205>

Here is the code

local physics = require( “physics” )

physics.start()

display.setDefault(“background”, 1, 0, 0)

function randomcolor()

local colors = { “2ecc71”, “3498db”, “9b59b6”, “2c3e50”, “e74c3c” }

    local  choosecolor = color[math.random(1,5)]    

    end

  

    local function addnewobject()

    local bombstartnew = display.newRect( 150, -340, 50, 50)

physics.addBody( bombstartnew )

bombstartnew.enterFrame = offscreen

   

   bombstartnew:setFillColor( convHexColor( choosecolors ))

Runtime:addEventListener( “enterFrame”, bombstartnew )

end

timer.performWithDelay( 500, addnewobject, 0 )

Hey cm2001m,

if the code you posted is correct it’s pretty simple.

You call the color table “colors” (plural) and try to index it with “color” (singular).

Also you should use code tags to make your code more readable.

Hey cm2001m,

if the code you posted is correct it’s pretty simple.

You call the color table “colors” (plural) and try to index it with “color” (singular).

Also you should use code tags to make your code more readable.