I need help solving this strange variable scope error

So, we all know the common error “local variable ‘yVel’ referenced before assignment”. I got this error, but this time it is different. I am referencing my “yVel” variable outside of the processing draw function, and inside the processing draw function I am saying “yVel += gravity”. I don’t know how to fix this because I cant put the variable in the draw function, because then it would just keep resetting the variable. And yes, I did try using python globals, and those didn’t work either. And, just to test, I defined the variable in the draw function, and it didn’t show another reference error for the “gravity” variable, so I know I can reference variables outside of the draw function. Here is my script:

width = 1000
height = 1000
#Variables
x = random(0,width)
y = random(0,height)
xVel = random(-10,10)
yVel = random(-0.5,0.5)
gravity = random(0,1)
elasticity = random(0,1)
radius = random(2.5,50)
friction = random(0,1)    
    


def draw():
    yVel += gravity
    x += xVel
    y += yVel
    if x<float(radius/2) or x>width-float(radius/2):
        xVel =- xVel*elasticity
    if x<float(radius/2) or x>width-float(radius/2):
        xVel =- xVel*elasticity
    if x < float(radus/2):
       x = float(radius/2)
    
    else:
        if x > width-float(radius/2):
            x = width-float(radius/2)
    
    if y < float(radus/2):
        y = float(radius/2)
        
    else:
        if y > width-float(radius/2):
            y = width-float(radius/2)
                            
    if y==height-float(radius/2):
        xVel *= friction
    
    ellipse(x, y, radius, radius)

Does anyone know why this is happening?

1 Like

i just realized why, you didnt define setup(), for some reason if you dont define that variables break.

global x, y, xVel, yVel, radius

1 Like