Help please

CirclePit = class()

function CirclePit:init()


    self.units = {}
    self.r = 600  
    delPi = math.rad(1)
    CirclePit:addUnit()
end

function CirclePit:addUnit()
    local angle = 0
    table.insert(self.units, Unit(angle))
end

Compiler says table.insert gets nil instead of table.
I did the same thing in other projects and it worked, what’s wrong here?

Upd

function setup()
    displayMode(FULLSCREEN)
    supportedOrientations(PORTRAIT_ANY)
    pit = CirclePit()
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(255, 255, 255, 255)

    -- This sets the line thickness
    sprite("Planet Cute:Rock", WIDTH/2, 120)
    noStroke()
    smooth()
    fill(255, 42, 0, 255)
    ellipse(WIDTH/2, -HEIGHT, HEIGHT *2 + 200)
    pit:draw()
    -- Do your drawing here
    
end


Also now it says that “pit” is nil.

You need self:addUnit(delPi) instead of CirclePit:addUnit().

It did not solve problem.

What about this?

CirclePit = class()

function CirclePit:init()
    self.units = {}
    self.r = 600  
    self.delPi = math.rad(1)
    self:addUnit(self.delPi)
end

function CirclePit:addUnit(angle)
    table.insert(self.units, Unit(angle))
end

Also, could we see the code for Unit? Is it in the same project?

Unit = class()

function Unit:init(angle)
    -- you can accept and set parameters here
    self.angle = angle
    self.state = 1
    self.r = {600, 580}
end

Yes, It’s in the same project.
But it still thinks that self.units is nil

@Vlayan Can we see the code where you use the CirclePit? Make sure you’re just doing something like myVariable = CirclePit() and NOT something like myVariable = CirclePit:init() or myVariable = CirclePit.init()

you need to do

self:addUnit()

instead of

CirclePit:addUnit()

(I mean not the function name but when you call it in the CirclePit:init() )

what you’re doing is creating an object, that object has the table units created, but then you call a function that calls self.units, but you need to call the function from that object you just created, which is done by using self:function()m

or if you’d need to call it outside of the class, it would be object:function()

in your case:

pit:function()

Thank you very much, stevon8ter, it solved the problem!
And also thanks for explanation.