Referenced before assignment

I’m getting a UnboundLocalError: local variable ‘rotor00’ referenced before assignment. error. I know about variable scoping, which is why this one confuses me.

This is a model of a motor, I’m swapping out parts to demonstrate how it works. rotor00 is is a blender object.

The affected code is…

if ‘rotTotal’ not in rotor:
start(rotor, scene)
rotor00 = scene.objects[‘rotor00’] #make the rotor00 object useable by the bge.
rotor00.setVisible(False,False)
print(‘set rotors’, rotor00.visible) # print out rotor00 visibility, it works properly, rotor00 has been set to False

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RKEY]: #read the keyboard
#rotate left
rotor.applyRotation((0,rotAngle,0), True)

if rotor[‘rotTotal’] == 0: #rotTotal controls which objects get displayed
rotor00.setVisible(False,False)

It throws the error flag on the last line of the code. If this were a function I could see why there would be a problem, but it’s only an if statement.

Any ideas on what is happening?

Thanks.

Script runs for the first time

if ‘rotTotal’ not in rotor: This if statement only runs once. start(rotor, scene)
rotor00 = scene.objects[‘rotor00’] #make the rotor00 object useable by the bge. You’ve declared the rotor00 variable inside this statement.
rotor00.setVisible(False,False)
print(‘set rotors’, rotor00.visible) # print out rotor00 visibility, it works properly, rotor00 has been set to False

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RKEY]: #read the keyboard#rotate left
rotor.applyRotation((0,rotAngle,0), True)

if rotor[‘rotTotal’] == 0: #rotTotal controls which objects get displayedrotor00.setVisible(False,False)Everything is okay.

Script runs once and rotor00 falls out of scope

Script runs for the second and subsequent times

if ‘rotTotal’ not in rotor: This statement does not run this time…start(rotor, scene)
rotor00 = scene.objects[‘rotor00’] #make the rotor00 object useable by the bge. Therefore this line decaring rotor00 never happened.
rotor00.setVisible(False,False)
print(‘set rotors’, rotor00.visible) # print out rotor00 visibility, it works properly, rotor00 has been set to False

if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.RKEY]: #read the keyboard#rotate left
rotor.applyRotation((0,rotAngle,0), True)

if rotor[‘rotTotal’] == 0: #rotTotal controls which objects get displayedrotor00.setVisible(False,False)Therefore, this variable doesn’t exist.

You’re declaring the rotor00 variable under an if block, so it can only come into existence if that code actually runs.

Get the rotor outside the if, before it’s used anywhere, and it should work.

*Edit

GGantzel is fast. :slight_smile:

This is less a problem of scope. The scope is always the same. It is a problem of process flow.

You declare the variable under one condition. (‘rotTotal’ not in rotor)
You use the variable under another condition. (rotor[‘rotTotal’] == 0)

Conclusion: if the the first condition fails and the second one passes, the code accesses an undeclared variable (as the error says).

More or less you have a dependency problem.

Solution:
Ensure the variable is ALWAYS declared before accessing the it.
There are several ways to do this. Example: define an “else” case towards the first condition. Or declare the variable outside the if statement as mentioned before.

Hmmm,

I was using the first condition because I wanted to initialize the object as a game object. And the program reliably went through that condition first. I didn’t want to have to keep reinitializing the object. I guess I goofed.

And yes, getting that out of the if statement, seemed to do it.

Thank you

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global. The unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. All variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.