NameError: global name 'mickey' is not defined

Hello I am having a problem with the code below…

class MickeyLoader:
	global mickey
	def __init__(self):
		global mickey
		self.mickey = Actor('phase_3/models/char/mickey-1200.bam',
		{'walk':'phase_3/models/char/mickey-walk.bam'})
		self.MickeyPosInterval1 = mickey.posInterval(3, Point3(60.5652, 1.53084, 4.025), startPos=Point3(5.91,0,4.025))
		self.mickey.pandaPlace = Sequence(MickeyPosInterval1)
		self.mickey.pandaPlace.loop()
	
	def MickeyWalk(self):
		global mickey

		
	def MickeyRender(self):
		global mickey
		self.mickey.reparentTo(render)
		self.mickey.loop("walk")	
		self.mickey.setPosHpr(5.91,0,4.025,270,0,0)

and i am getting this error:

DirectStart: Starting the game.
Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
Traceback (most recent call last):
  File "main.py", line 483, in <module>
    mic = MickeyLoader()
  File "main.py", line 445, in __init__
    self.MickeyPosInterval1 = mickey.posInterval(3, Point3(60.5652, 1.53084, 4.025), startPos=Point3(5.91,0,4.025))
NameError: global name 'mickey' is not defined
Press any key to continue . . .

please help… i have tried over and over. :laughing:

You defined the variable as “self.mickey” (which indicates that it’s an instance variable of the containing class), but are attempting to access it as simply “mickey”–in other words, you left off the “self.” prefix, I believe.

Now i am getting another ERRor

class MickeyLoader:
	global mickey
	def __init__(self):
		global mickey
		self.mickey = Actor('phase_3/models/char/mickey-1200.bam',
		{'walk':'phase_3/models/char/mickey-walk.bam'})
		self.MickeyPosInterval1 = self.mickey.posInterval(3, Point3(60.5652, 1.53084, 4.025), startPos=Point3(5.91,0,4.025))
		self.mickey.pandaPlace = Sequence(MickeyPosInterval1)
		self.mickey.pandaPlace.loop()
DirectStart: Starting the game.
Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
Traceback (most recent call last):
  File "main.py", line 483, in <module>
    mic = MickeyLoader()
  File "main.py", line 446, in __init__
    self.mickey.pandaPlace = Sequence(MickeyPosInterval1)
NameError: global name 'MickeyPosInterval1' is not defined
Press any key to continue . . .

nevermind I fixed it THANK YOU :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

Also note that you don’t need to write “global mickey” in every function. For one thing, you have nothing defined as mickey, but rather self.mickey. All variables preceded by “self” within a class can be referenced anywhere throughout the class without global declaration. (That includes other functions in the class.)