Path in factory.create() without parent specified sometimes throws error (SOLVED)

I have a script that spawns a star with the following function:

function spawn_star()
	local position = go.get_position()
	position.x = sys.get_config("display.width")
	position.y = math.random(60, 600)

	if math.random() < 0.05 then
		factory.create("#booster_factory", position)
	end
end

This works for the most part but sometimes it throws the error “Component could not be found”. I have not found a pattern for when it throws the error but it seems to be random.

The really interesting thing is that the problem disappears if I specify the parent object in the path. This example is working as expected:

function spawn_star()
	local position = go.get_position()
	position.x = sys.get_config("display.width")
	position.y = math.random(60, 600)

	if math.random() < 0.05 then
		factory.create("factories#booster_factory", position)
	end
end

Is there something that I am missing that causes this or is it some kind of bug? It is maybe a known issue?

Thanks!

Where do you call this function from? Probably from some other gameobject’s script.

You’ve defined it as a global function, so every other script may call it. Make sure you carefully read the section of the lua manual about the scope of variables. https://www.defold.com/manuals/lua/#_locals_globals_and_lexical_scoping

Oh yeah, of course… I was not aware that someone else accidentally called my function. Thanks!