Hello! I’m getting this as an error message: “attempt to index global ‘box1GreenButton’ (a nil value)” when I try to delete box1GreenButton using box1GreenButton:removeSelf (). At the top of my code, I have a clickable button that makes a statement true and then creates box1GreenButton as a display object. Further down, I have another clickable button that starts a function and if the previous statement is true, it attempts to remove the object. That’s when I get the error message. Can you provide any help on this? I’m at a loss because I don’t understand how it could be nil if it’s on the screen. Any help is appreciated. [import]uid: 35535 topic_id: 31931 reply_id: 331931[/import]
Nate112:
You’d need to post some code, but I suspect that you have a “box1GreenButton” that was created locally inside of a function. That means that if you have a reference to it outside the function, it’s not going to pick it up.
Try something like this:
[lua]local box1GreenButton – Declare it locally at the top
local function createButton()
box1GreenButton= --Create it, however you want to do it, UI, widget, whatever, but DON’T LOCALIZE IT!
end
local function pressTheSecondButton()
display.remove(box1GreenButton) – Display.remove is better, also.
end[/lua]
That should work. Of course, you’d be doing more than that; that is only an example. [import]uid: 147322 topic_id: 31931 reply_id: 127460[/import]
Quinc is right. I would also suggest two things:
- Put some error detection code in your app to detect a nil value:
if( box1GreenButton ~== nil ) then -- Not nil, now we can delete it
box1GreenButton:removeSelf()
box1GreenButton = nil -- Now set it to nil to be sure it gets removed by garbage collector
else
print("ERROR! -\> Hey box1GreenButton is nil!")
end
- If your error testing code trips, you’ll see it your console. Now you can start adding code to your app to track the problem back to its root. Basically, you can use a editor/debugger (Cider, etc.) or you can use print statements at places you may have modified/deleted the pointer to your object.
I hope this helps a little.
Cheers,
Ed
[import]uid: 110228 topic_id: 31931 reply_id: 127489[/import]
Nate112:
You’d need to post some code, but I suspect that you have a “box1GreenButton” that was created locally inside of a function. That means that if you have a reference to it outside the function, it’s not going to pick it up.
Try something like this:
[lua]local box1GreenButton – Declare it locally at the top
local function createButton()
box1GreenButton= --Create it, however you want to do it, UI, widget, whatever, but DON’T LOCALIZE IT!
end
local function pressTheSecondButton()
display.remove(box1GreenButton) – Display.remove is better, also.
end[/lua]
That should work. Of course, you’d be doing more than that; that is only an example. [import]uid: 147322 topic_id: 31931 reply_id: 127460[/import]
Quinc is right. I would also suggest two things:
- Put some error detection code in your app to detect a nil value:
if( box1GreenButton ~== nil ) then -- Not nil, now we can delete it
box1GreenButton:removeSelf()
box1GreenButton = nil -- Now set it to nil to be sure it gets removed by garbage collector
else
print("ERROR! -\> Hey box1GreenButton is nil!")
end
- If your error testing code trips, you’ll see it your console. Now you can start adding code to your app to track the problem back to its root. Basically, you can use a editor/debugger (Cider, etc.) or you can use print statements at places you may have modified/deleted the pointer to your object.
I hope this helps a little.
Cheers,
Ed
[import]uid: 110228 topic_id: 31931 reply_id: 127489[/import]