Assigning myName to an element in an array causes an error?

  
firstCrate = #allCrates[1]  
firstCrate.myName = "crate1"  
  

attempt to index global ‘firstCrate’ (a number value)

Can someone explain why, and how to overcome this error, please.

I need to reference the item in the table this way to check for a collision.

[import]uid: 68741 topic_id: 13708 reply_id: 313708[/import]

the error is not in [lua]firstCrate.myName = “crate1”[/lua]
but in [lua]firstCrate = #allCrates[1][/lua]

what is #allCrates[1] ?
if allCrates is a table #allCrates is number of elements in it.
so not sure what you are expecting to get from #allCrates[1].
is it a multidimensional table ?

have you declared firstCrate somewhere ?
just try [lua]local firstCrate = #allCrates[1] [/lua] [import]uid: 71210 topic_id: 13708 reply_id: 50340[/import]

Thanks for the response…

it’s an array defined as:

 local allCrates = {}   

I’ve tried that I get the same error.

Just to clarify am I inserting crates into the array correctly?

crate = display.newRect( 0, 0, 30, 30)  
table.insert(allCrates, crate)   
local firstCrate = #allCrates[1]  
firstCrate.myName = "crate1"  

I’m trying to give the first crate in the array a value that I can refer to it by, then set its myName to something so I can test for collisions on it.

EDIT:

I removed the #, and removed local firstCrate from the scope, which fixed all other errors. Thanks! [import]uid: 68741 topic_id: 13708 reply_id: 50342[/import]

will you be creating multiple crates in a loop ?

if you do like this
[lua]local firstCrate = allCrates[1]
firstCrate.myName = “crate1”[/lua]
aren’t you giving the name crate1 always to the first item in allCrates ?

try doing this instead
[lua]crate = display.newRect( 0, 0, 30, 30)
crate.myName = “crate1”
table.insert(allCrates, crate) [/lua] [import]uid: 71210 topic_id: 13708 reply_id: 50346[/import]

notts_forest_,

the problem is the first line itself, even if you defined it as local… or global.

[lua]firstCrate = #allCrates[1]
firstCrate.myName = “crate1”[/lua]

what this does is creates a variable called firstCrate of type number, now you should know that numbers are not arrays, so to create an array, you need to use
[lua]firstCrate = {}
firstCrate.myName = “crate1”[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13708 reply_id: 50372[/import]