Why are not all of my factories spawning? (SOLVED)

Hello guys!
I have factory prefab to create cars on roads, but if I place more than one in the collection, only the first works and creates cars. What’s wrong? I’m new at Defold so I’m not sure where the problem could be.
Here some screenshots:

58 18

local variables in .script or .gui_script scopes are shared between instances of the script.
In you code the variables period and cooldown are shared, that means that every time when coldown is less then 0 you increase it for all the instances of this script and the other instances do not spawn object until the next time when condition cooldown <= 0 is true.

The simplest way to fix if is using self.cooldown and self.period instead of local variables.

Thanks! Now it’s working

1 Like

An improvement to get rid of the update() function is to use a timer instead:

local function spawn(self)
   -- note: you can skip passing go.get_position() and rotation since the spawned objects will inherit position and rotation from this object 
   factory.create("#factory", go.get_position(), go.get_rotation())
end

function init(self)
    -- call the spawn function repeatedly (true) every "cooldown" seconds
    timer.delay(cooldown, true, spawn)
end
4 Likes

Welcome to the forum by the way!

1 Like

Thanks