addObject python code bug - won't spawn with object hidden or shown.

I haven’t been able to get theaddObject python script to work.
I think this might be a bug that’s new in 2.64… or maybe I’m just failing here somewhere.

scene setup:

null named shooter in layer 1

sensors: delay 10 connected to
controller: python - running shooter.py (this file)

cone named bullet in layer 2

the problem is that if we show layer 2, we get a value error
which says " scene.addObject(object,other,time): KX_Scene (first argument):
object must be set in an inactive layer

but, if we hide layer 2, then we get another error:

Key Error: “CList[key]: ‘‘bullet’’ key not in list”

'cuz it’s in a hidden layer.

I really need this to work so I can do stuff with my objects
after I spawn 'em… OR in general, spawn them from the
script.

import bge

sce = bge.logic.getCurrentScene()

shooter = sce.objects["shooter"]
bullet = sce.objects["bullet"]

shell = sce.addObject(bullet, shooter, 0)

add object error.blend (381 KB)

As indicated in the API, to add an Object on a hidden layer, do not give a reference to it (you should not be able to anyway). Instead pass the name of the object as a string

Hi, this should work:

shell = sce.addObject(“bullet”, shooter, 0)

Thanks!! – it worked, once I commented out the part where I defined the bullet.

import bge
sce = bge.logic.getCurrentScene()
shooter = sce.objects[“shooter”]
#bullet = sce.objects[“bullet”]
shell = sce.addObject(“bullet”, shooter, 0)

please feel free to mark this as resolved. =)