python not recognising an objects property

ok here’s my problem. i have one object that uses a script, in this script i define another obj like this.

cont = logic.getCurrentController()
sce = logic.getCurrentScene()

objList = sce.objects

objR = objList[“Player2”]

on this other object, Player2, i have already defined a property in this way

obj[‘crash’] = 0

so back to my original script, i use a statement like this. this statement has worked for me before and i see no problems with it.

if objR[‘crash’] == 0:

anyways i would think that this should work, and it does with many of my other scripts, however python pops up an error kinda like this.

if objR[‘crash’] == 0:
KeyError: value = gameOb[key] KX_GameObject, key “crash” does not exist

any help would be appreciated, i have no idea why this dosn’t work.

I don’t know if this will help, but are the objects set to be Actors in the physics panel?
and shouldn’t that be: “objR[‘crash’] = 0”
instead of: “obj[‘crash’] = 0” ?

It may be a case error, do note that everything is case sensitive in python.

Also, make sure that Player2 is actually in the scene when the script runs, if he gets created after the scripts start running or he gets destroyed while it is running then you will get an error unless you check to see if he exists before checking the property.

Mostlikely it is a problem with the execution order. The property is not set when reading it.

Solutions:

A) set the property in the property panel. Then it will be there right from start.
B) surround the read with a try…except block (check Python syntax)
C) use get and check for None


crash = objR.get('crash') 
if crash is None:
  return

thanks Monster, i guess for some reason the property wasn’t made in time. i set the property in the property panel and it works just fine.