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:
local variables in
.gui_script scopes are shared between instances of the script.
In you code the variables
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.period instead of local variables.
Thanks! Now it’s working
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
Welcome to the forum by the way!