Setting GLTF Model Asset Undefined

I’m encountering a TypeScript error while following the official documentation on how to use GLTF assets.

I follow the GLTF loading example directly from your docs, and the code looks like this:

import * as ecs from '@8thwall/ecs'

ecs.registerComponent({
  name: 'random_luckyitem',
  schema: {
    // @asset
    model1: ecs.string,
    // @asset
    model2: ecs.string,
    // @asset
    model3: ecs.string,
  },

  add: (world, eid, component) => {
    const modelOptions = [component.model1, component.model2, component.model3]
    const validModels = modelOptions.filter(Boolean)
    const selected = validModels[Math.floor(Math.random() * validModels.length)]

    ecs.GltfModel.set(world, eid, {
      url: selected,
    })
  },
})

However, when I compile the project, I get the following error:

Unhandled promise rejection: TypeError: undefined is not an object (evaluating ‘component.model1’)

Please help me clarify.

It’s because you’re trying to access the Schema directly from the component variable. You should be using StateMachines and SchemaAttribute as well. It will help a ton with keeping your code organized.

Here’s an example (SchemaAttribute and getting schema values from the component):

Thanks for your reply. It works :grinning_face:

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.