How to assign number to each object created

I have a game where it spawns objects every 0.5 seconds how can i assign number to each object created and store it in a table to use it later?

Here is code:

local function addnewobject()

  

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

physics.addBody( bombstartnew )

    bombstartnew.fillColor = {red, green, blue}

end

timer.performWithDelay( 500, addnewobject, 0 )

I’ve tried to comment as simply as possible:

--create the table local myBombTable = {} --create a number to assign local bombCount = 0 local function addnewobject() local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) --increase number of bombs bombCount = bombCount + 1 --add bomb to myBombTable, using the number above as the index myBombTable[bombCount] = bombstartnew --you could also have done this instead: myBombTable[#myBombTable+1] = bombstartnew --which means find the current length of the table, and insert bomb into the next index --you can also assign the number directly to the bomb if you want to, to be referenced later bombstartnew.myNumber = bombCount bombstartnew.fillColor = {red, green, blue} end timer.performWithDelay( 500, addnewobject, 0 ) --print number of bombs every 5 seconds local function printSomething() print("Number of bombs = ", bombCount) end timer.performWithDelay( 5000, printSomething, 0 ) local function doSomething() --get a random number between 1 and however many bombs we have local myRandomNumber = math.random(1, bombCount) --change the color of the bomb that sits at that index in the bombTable myBombTable[myRandomNumber]:setFillColor(math.random(), math.random(), math.random()) --print the number that was assigned print("my number is ", myBombTable[myRandomNumber].myNumber) end timer.performWithDelay( 10000, doSomething, 0 )

Also, in future when you paste code into the forum can you use the blue <> button, as it makes the code more readable.

how can you print color of object when clicked ?

Thanks

I’ve tried to comment as simply as possible:

--create the table local myBombTable = {} --create a number to assign local bombCount = 0 local function addnewobject() local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) --increase number of bombs bombCount = bombCount + 1 --add bomb to myBombTable, using the number above as the index myBombTable[bombCount] = bombstartnew --you could also have done this instead: myBombTable[#myBombTable+1] = bombstartnew --which means find the current length of the table, and insert bomb into the next index --you can also assign the number directly to the bomb if you want to, to be referenced later bombstartnew.myNumber = bombCount bombstartnew.fillColor = {red, green, blue} end timer.performWithDelay( 500, addnewobject, 0 ) --print number of bombs every 5 seconds local function printSomething() print("Number of bombs = ", bombCount) end timer.performWithDelay( 5000, printSomething, 0 ) local function doSomething() --get a random number between 1 and however many bombs we have local myRandomNumber = math.random(1, bombCount) --change the color of the bomb that sits at that index in the bombTable myBombTable[myRandomNumber]:setFillColor(math.random(), math.random(), math.random()) --print the number that was assigned print("my number is ", myBombTable[myRandomNumber].myNumber) end timer.performWithDelay( 10000, doSomething, 0 )

Also, in future when you paste code into the forum can you use the blue <> button, as it makes the code more readable.

how can you print color of object when clicked ?

Thanks