parent to nearest property

What is wrong with this parent to nearest property script?

from bge import logic


CODE]scene = logic.getCurrentScene()
own = logic.getCurrentController.owner()
prop = "property"
distance = 10000
closestObject = None
for object in scene.objects:
    if prop in object:
        dist = own.getDistanceTo(object)
        if dist < distance:
            distance = dist
            closestObject = object
own.setParent(closestObject)

“own = logic.getCurrentController.owner()”

No parentheses after owner, but you need parentheses after “getCurrentController”

So it should read:
logic.getCurrentController().owner

It did not work and there is no error in the console.

I think that you must not have the property named correctly. I would double check the spelling AND capitalization for the property that you are checking for. ClosestParent.blend (503 KB)

if there is no console output, add some print statements to see if the code is executed.

What i need is the cube with the python controller to be the main parent.That is why i did not see it working the first time.

We’re encouraging you to stop asking for help and help yourself. How do you think we solve problems? The same way you can! Use print statements to write to the console and then compare what you expect to see with what you do

Sent from my Nexus 5 using Tapatalk

If you want the cube that runs the script to be the parent, then you need to change

own.setParent(closestObject)

To:

closestObject.setParent(own)

You may find this resource useful for any future projects. It is documentation for python specific to Blender. And like agoose77 said, it tends to be better to figure something out for yourself, however if you really do get stuck then there are many people that will be willing to help.

Python documentation specific for game objects (I figured it would be the most useful section):
http://www.blender.org/documentation/blender_python_api_2_64_release/bge.types.html#bge.types.KX_GameObject

My destructible environment prototype.Press a to go forward b to rotate.W to add block to delete cube.Then press x to delete cube in ground.

block parent.blend (500 KB)