             if active_texture != bound_unit
                 @active_texture = bound_unit
                 gl.activeTexture gl.TEXTURE0 + bound_unit
+            old_tex = bound_textures[bound_unit]
             if old_tex?
                 old_tex.bound_unit = -1
                 if old_tex.gl_target != texture.gl_target

                    # this means it's the probe cube texture
                    tex = mesh.probe?.cubemap or @blank_cube_texture
                    tex_input.value = tex
                else
                    tex = tex_input.value
                if tex.bound_unit == -1

        @background_alpha = if ba? then ba else 1
        @compiled_shaders_this_frame = 0
        @use_frustum_culling = true
        @unbind_textures_on_draw_viewport = true
        @probes = []

