// Here we have a script that aims to store 3D mesh objects in a map
// The problem arises when attempting to access the stored object later
this.meshMap = new Object();
// Function to add a mesh to the map using a specified key and URL
this.addMesh = function(key, url) {
var map = this.meshMap; // Reassigning the map variable to be used locally
// Using THREE.JSONLoader to load the mesh from the provided URL
var loader = new THREE.JSONLoader();
loader.load(url, function(geometry) {
// Storing the loaded mesh as an object in the map using the key
map[key] = new THREE.Mesh(geometry, material);
// Adding the mesh object to the scene
scene.add(map[key]);
});
// Attempting to set the position of the mesh after it has been loaded
// However, encountering an error due to the object being null at this point
map[key].position = new THREE.Vector3(0, -10, 0);
}
What is the correct way to store the mesh in the map so that it can be accessed without errors?