You need to take care of the timing.
Within a single frame (over the complete scene):
- sensors evaluation
- controllers execution
- actuators running
- physics, render etc.
The code always runs with "controllers execution". This does not mean that all operations have immediate effect.
Activating an actuator, does not make the actuator running at the moment of the call. The actuator gets a signal to activate. The actuator will run within "actuators running". Which is later than your code.
KX_SCA_AddObjectActuator.objectLastCreated provides you a reference to the last created object by that actuator. That can be last frame or any frame before.
Within the first execution of your Python controller the actuator never added a new object. This means you get None. I expect you get an error each time you trigger the controller the first time as None has no "worldPosition" (always have the console window open when you run Python code with the BGE).
There are several ways to solve the timing issue.
A) Two step processing. You store the position and adjust the position of the new object within the next frame. The drawback is, that you can see the unchanged position for a single frame. This can be annoying.
B) Do not activate the actuator, but let it add the object via instantAddObject(). AFTER that call objectLastCreated will return the just created object.
You code could look like that (supposed to run in script mode):
import bge
controller = bge.logic.getCurrentController()
mouseSensor = controller.sensors["Ray"]
activationSensor = controller.sensors["Clik"]
if activationSensor.positive and
mouseSensor.positive:
addActuator = controller.actuators["add"]
addActuator.instantAddObject()
addedObject = addActuator.objectLastCreated
addedObject.worldPosition = mouseSensor.hitPosition