Error occurs 90% of the time…

I’m so confused as to why this error happens…it sometimes does this:

It sometimes works and does this:

Are you detecting when all assets have loaded using LoadingManager you may be trying to access a models position before it has been fully loaded…

1 Like

I’m not using a loading manager…however, could you elaborate…

If it is helpful I’ll copy and paste my code in for you.

Any solutions are appreciated…

Uncaught TypeError: Cannot read property of undefined is a common error, it simply means you are trying to access a property of an object that is undefined. in your case myObj.position, myObj is undefined.

Why? most likely what @Lawrence3DPK suggested, trying to access an object before the loading complete or instantiation … sharing your code would be helpful!

2 Likes

Both @Lawrence3DPK and @Fennec already described the problem. Let me add my two cents.

Most likely, you load a model (let’s say into variable model) and in the animation loop you change its position via model.position.

The problem is that loading is asynchronous, i.e. you request loading, but at the time the animation loop is activated, the object is still not loaded.

The easiest way to fix this (if this is the actual problem) is to use if to safeguard against missing model, like this:

if ( model ) { model.position.x = 5; }

Another way to fix is to start the animation loop after the model is being loaded.

1 Like

The best way to overcome this kind of errors is to use typescript or even better vite + typescript, the line will go red with a potential explanation highlighted, before you even run you code.