near sensor code

What is wrong with this code it shows no error but doesn’t work?It uses a near sensor and it assigns a property to it.

import bge
from bge import logic
cont = logic.getCurrentController() 
obj = cont.owner
sens = cont.sensors["Mole"]
sen.propName = "Knife" 
actuator = cont.actuators["Enemy"]
if sens.positive:
        cont.activate('Enemy')
else:
        cont.deactivate('Enemy')

if sens.positive == True:

May help

but it sounds to me more like you have a logic error rather than a python one.
What are you trying to do, and what isn’t working?

do you have some object called ‘sen?’ i see a ‘sen.propname’ it’s hard to tell being out of the context of what you are actually doing. should it be ‘sens.propname?’

‘if sens.positive:’ is fine for checking True

A) enable physics vizualization in game menu to see the sensors range
B) add a print before ‘if…’ To see if the controller is triggered at all.
C) be aware stitic onjects need the actor flag enabled to get detected (physics settins)

Here’s the blend.

Attachments

python b.blend (1.73 MB)

sen.propName = "Knife"

You left off the ‘s’ on “sens”.

This is the console error.

  File "st pube.py", line 9, in <module>
AttributeError: 'KX_NearSensor' object has no attribute 'properyName'
Python script error - object 'Cube.002', controller 'And':
Traceback (most recent call last):
 

This is the corrected python script.

import bge
from bge import logic
cont = logic.getCurrentController() 
obj = cont.owner
sens = cont.sensors["Mole"]
sens.properyName = "Knife" 
actuator = cont.actuators["Enemy"]
if sens.positive == True:
        cont.activate('Enemy')
else:
        cont.deactivate('Enemy')
        
        
        


Use “propName”, not “propertyName”

Now there is no error and it is still not working.The code works i had to put the dynamic object underneath on a plane.That is what made it work.

Is there a better way to trigger a steering actuator with a near sensor?I would like it to be simple though.I am trying to learn to program the blender game engine.