Use var created in function outside function

I have this code

local function addnewobject() local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} end

How do i use 

bombstartnew.fillColor

outside of the function 

local bombstartnew local function addnewobject() bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} end

does not work get nil error 

Can you show all of your code?  

Also, I would highly recommend reading about scope here

local bombstartnew local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} end print(bombstartnew.fillColor) -- does not work timer.performWithDelay(500, addnewobject,0)

You didn’t quite do as I showed in my first reply.  Please look carefully.

And again, it will be very helpful for you to read the tutorial about scope. 

what did you say to do in first reply beside show all code and look at scope

here is full code

local ragdogLib = {}; ragdogLib.convertHexToRGB = function(hexCode) assert(#hexCode == 7, "The hex value must be passed in the form of #XXXXXX"); local hexCode = hexCode:gsub("#","") return tonumber("0x"..hexCode:sub(1,2))/255,tonumber("0x"..hexCode:sub(3,4))/255,tonumber("0x"..hexCode:sub(5,6))/255; end return ragdogLib; local ragdogLib = require "ragdogLib"; function randomcolor() local colors = { "#e67e22","#e74c3c" ,"#f1c40f","#1abc9c","#8e44ad" } local finalcolor = colors[math.random(1, 5)] return finalcolor end local bombstartnew local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} end print(bombstartnew.fillColor) -- does not work timer.performWithDelay(500, addnewobject,0)

What I meant was to look at my code more closely.  Remove “local” from your second bombstartnew declaration.

still get Attempt to index local ‘bombstartnew’ (a nil value) error

There are numerous issues with the code you posted.  Sorry I can’t go through all of them.   However, the reason you’re getting the nil error with bombstartnew is because you are trying to print bombstartnew.fillColor before bombstartnew has even been created in your addNewObject function

i put 

print(bombstartnew.fillColor)

after timer.performWithDelay(900, addnewobject,0)

but still get same error

That’s because addnewobject() is run 500 millseconds later. The print statement happens immediately after the timerPerformWithDelay() executes.

Rob

Rob

i have assigned color to every object is created, when i tap the object it gives color but when i create bombstartnew local outside adddobject function and when i tap it, it gives me wrong color

Gives correct color when tocuhed 

local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombCount = bombCount + 1 myBombTable[#myBombTable+1] = bombstartnew bombstartnew.myNumber = bombCount local function bombTouched(event) if ( event.phase == "began" ) then print(unpack(bombstartnew.fillColor)) Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score + 1 -- add new score to current score scoreText.text = score end end bombstartnew.enterFrame = offscreen Runtime:addEventListener( "enterFrame", bombstartnew ) bombstartnew:addEventListener( "touch", bombTouched ) end

wrong color when touched

local bombstartnew local function bombTouched(event) if ( event.phase == "began" ) then print(unpack(bombstartnew.fillColor)) Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score + 1 -- add new score to current score scoreText.text = score end end local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombCount = bombCount + 1 myBombTable[#myBombTable+1] = bombstartnew bombstartnew.myNumber = bombCount bombstartnew.enterFrame = offscreen Runtime:addEventListener( "enterFrame", bombstartnew ) bombstartnew:addEventListener( "touch", bombTouched ) end

cm2001m, like JonPM suggested, you should read how scope works first. you should simplify you code understand it while the code is evolving.

you waste more time trying to correct the code than reading the manual first then do the correct code.

saying that, i’ve made a working game of random objects droping from the ceiling based on your code:

local physics = require( "physics" ) physics.start() local score=0 local rand=math.random ----- score local params={ text="Points: "..score, fontSize=15, align="center" } local text=display.newText(params) text.x=display.contentWidth\*.5 text.y=display.contentHeight-25 ------ ------ remove objects offscreen not touched local function offscreen(self) if self.y\>display.contentHeight then Runtime:removeEventListener( "enterFrame", self ) self:removeSelf() self=nil print ("objected removed") end end ----- touch event local function bombTouched(self, event) if ( event.phase == "began" ) then print(unpack(self.fillColor)) Runtime:removeEventListener( "enterFrame", self ) self:removeSelf() self=nil score = score + 1 -- add new score to current score text.text="Points: "..score end end ---- create object local function addnewobject() local startingpoint = rand(display.contentWidth\*0.1,display.contentWidth\*0.9) local bombstartnew = display.newRect( startingpoint,-100, 50, 50) physics.addBody( bombstartnew) local color1, color2, color3 = rand(), rand(), rand() bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombstartnew.touch=bombTouched bombstartnew:addEventListener( "touch", bombstartnew ) bombstartnew.enterFrame = offscreen Runtime:addEventListener( "enterFrame", bombstartnew ) end timer.performWithDelay(300,addnewobject,50)

there are 1000 ways to do this, this is just one of those.

but when using self its imposible to compare two objects color that are in diffrent function 

local bombstartnew local function addnewobject() bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} end

does not work get nil error 

Can you show all of your code?  

Also, I would highly recommend reading about scope here

local bombstartnew local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} end print(bombstartnew.fillColor) -- does not work timer.performWithDelay(500, addnewobject,0)