Exception thrown whenever I try to load a Model from a list

56 views Asked by At

Cannot access a disposed object. Object name: 'BasicEffect'.

This exeption is thrown whenever I try to load a model from a list. The method used to work but has recently failed and I do not know why.

Below is my render code:

public void RenderModel(Model m, Vector3 ModelPosition,  float rotY = 0, float rotX = 0, float rotZ = 0, bool lit = true, bool collision = false)  
{  
    if (m != null)  
    {  
        Matrix[] transforms = new Matrix[m.Bones.Count];    
        m.CopyAbsoluteBoneTransformsTo(transforms);  

        float rotationZ = rotZ * (float)(Math.PI / 180);
        float rotationY = rotY * (float)(Math.PI / 180);
        float rotationX = rotX * (float)(Math.PI / 180);

        foreach (ModelMesh mesh in m.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                if (lit == true)
                {
                    //effect.EnableDefaultLighting();
                }
                effect.World = transforms[mesh.ParentBone.Index] *
                    Matrix.CreateRotationY(rotationY) *
                    Matrix.CreateRotationX(rotationX) *
                    Matrix.CreateRotationZ(rotationZ) *
                 Matrix.CreateTranslation(ModelPosition);
                effect.View = view;
                effect.Projection = proj;
            }
            mesh.Draw();
        }
    }
}

And then my list code (Path being the path of the model)

public void AddModel(string Path)  
{  
    Model newModel;  
    newModel = Content.Load<Model>(Path);  
    renderModels.Add(newModel);  
}

And finally the Draw code, Put in the draw method.

foreach (Model m in renderModels)  
{  
    camera.RenderModel(m, new Vector3(0,0,0));  
}  

Just to clarify, I need this for a level editor for a game my small team have been working on. Any help is much appreciated.

0

There are 0 answers