Need help removing a object attach to the same object.

Hi, ok so basically my problem is trying to remove an objecct attach to an object. Ok so basically I have plane spawning every 1 second and is flying across the screen. As the plane is flying across the scene is it has the little circles on attach to the plane. So when the plane is removed I get an error saying this

Runtime error
/Users/sebitttas/Desktop/Helicopter pixel/menu.lua:55: attempt to index upvalue ‘enem4’ (a nil value)
stack traceback:
[C]: ?
/Users/sebitttas/Desktop/Helicopter pixel/menu.lua:55: in function ‘_listener’

The error goes to this code

[code]
local wall = display.newRect(-10, 0, 10, 320)
wall.hit = “wall”
localGroup:insert(wall)
physics.addBody(wall ,“static”)

local function enemies4 ()
particle = false
local enem4 = display.newRect(0,0,100, 100)
enem4.y = math.random(30, 290)
enem4.x = 600
enem4.isFixedRotation = true
physics.addBody (enem4, {bounce=0.6, isSensor = true})
transition.to (enem4, {time = 5000, x=-50})
enem4.hit = “enem4”
enem4.hits = 0
localGroup:insert(enem4)
enem4:addEventListener(“collision”, enem4)
function enem4:collision (event)
if event.other.hit == “wall” then
enem4:removeSelf()
enem4 = nil
particle = true
end
if event.other.hit == “heli” then
display.remove(enem4)
Life = Life - 1
health.text = “Health:”… Life
enem4 = nil
end
end
local badtwin = display.newRect(0,0 , 10, 10)
badtwin.x = enem4.x - 30
badtwin.y = enem4.y
badtwin.hit = “badtwin”
physics.addBody (badtwin, {bounce=0.6})
localGroup:insert(badtwin)
transition.to (badtwin, {time = 3000, x= - 200})
badtwin:addEventListener(“collision”, badtwin)
function badtwin:collision (event)
if event.other.hit == “wall” then
badtwin:removeSelf()
badtwin = nil
end
if event.other.hit == “heli” then
badtwin:removeSelf()
Life = Life - 1
health.text = “Health:”… Life
badtwin = nil
end
end
local function good()
if particle == false then
local circle = display.newCircle(0,0,3)
circle:setFillColor(math.random(103),math.random(73),math.random(136))
circle.x = enem4.x + 5
circle.y = enem4.y
physics.addBody (circle, {bounce=0.6, isSensor = true})
circle:addEventListener(“collision”, circle)
function circle:collision (event)
if event.other.hit == “wall” then
circle:removeSelf()
circle = nil
end
end
local x1 = enem4.x + math.cos(math.random(360)) * math.random(5)
local y1 = enem4.y + math.cos(math.random(360)) * math.random(20,30)
transition.to(circle,{x = x1,y = y1,alpha = 0,time = 500,
onComplete = function() circle:removeSelf() circle = nil end })
enem4:toFront()
end
end
particles = timer.performWithDelay(1, good, -1)

end
bad4 = timer.performWithDelay(2000, enemies4, -1)[/code]

By the way the code is a plug. So what I’m trying to remove is spawn two objects at the same time spawning the circle and the plane which is the square, and removing them at the same time since the circle is at the same coordinate as the plane
[import]uid: 17058 topic_id: 22771 reply_id: 322771[/import]

You seem to have multiple objects called “enem4” on screen at once, have you considered spawning in a table? [import]uid: 52491 topic_id: 22771 reply_id: 90870[/import]

@peach no what I’m doing is enem4 spawns, when it spawns it spawns with bullets shooting, and small little circles at the same time. But when enem4 is removed I get the error. You can try the code is plug and play, and see what I mean. [import]uid: 17058 topic_id: 22771 reply_id: 90873[/import]

@peach so what should I do? [import]uid: 17058 topic_id: 22771 reply_id: 90886[/import]

enem4 is being removed and there are multiple instances of it on screen at once. If you used a table this wouldn’t happen.

It’s trying to get the x and y or enem4 but they don’t exist any more. [import]uid: 52491 topic_id: 22771 reply_id: 90903[/import]

–Removed-- [import]uid: 17058 topic_id: 22771 reply_id: 90904[/import]

@peach I how would I turn that into a table I’am really horrible at tables. [import]uid: 17058 topic_id: 22771 reply_id: 90905[/import]

Something like;

[lua]local enemy = {}

local function spawn()
enemy[#enemy+1]=display.newImage(“myimage.png”)
enemy[#enemy].x = 100
enemy[#enemy].y = 100
–etc etc
end[/lua]

Like that. You call that ten times and you’d have ten different objects. [import]uid: 52491 topic_id: 22771 reply_id: 90916[/import]