add object to position vector

How would i add a cube to position vector when distance is reached.Here is what i tried.

import bge

def main():

    cont = bge.logic.getCurrentController()
    scene = bge.logic.getCurrentScene()
    player = scene.objects ["player"]
    obj = cont.owner

    sens = cont.sensors['mySensor']
    actu = cont.actuators['myActuator']
    if obj.getDistanceTo(player) > 9:
      obj = scene.addObject("lung")
      obj.worldPosition = [2,8,9]

Can anyone figure out the problem i am having?

Hello. Try:
Remove obj.worldPosition
use player as parent instead.

I had to move the cube that the script was on by parenting it to another cube.And then moving that cube with logic bricks.

Yes. This line is invalid:

obj = scene.addObject("lung")

It should be, according to the API: [addObject(object, reference, time=0)](http://addObject(object, reference, time=0))
So for your code, it could be:

obj = scene.addObject("lung", player, 0)

Your link does not work.