Skip to main content
EchoExclusive
Inspiring
March 26, 2025
Solved

Attaching Scripts to Assets

Hi Creators!

I am using Desktop Editor and Typescript.

I am trying to spawn enemies, when the enemies spawn they will move around and look for the player, enemies can also get destroyed by bullets. The enemy is a 3D model in my assets. I can spawn the 3D model. I can also make the enemy move IF the enemy is already in the hierarchy and I attached a move/collision scripts I created. 

What I can't figure out is how do I attach the move script to the ENEMY ASSET (Similar to attaching scripts to a prefab in Unity).  So that when the enemy is spawned, it starts moving? There is no script option unless I drag the enemy asset to the hierarchy, but then that means I'm editing a copy and not the original asset.

 

 

Thanks!

gausroth
gausrothAnswer
Honored Contributor
March 26, 2025

If you want the script on the asset at spawn time. You will need to either create a copy of the asset with the script already attached or update the current asset after attaching the script. Let me see if I can get some screenshots.

EDIT: If you right click on the asset, click on Edit Template Definition then you should be able to add the script and save the change.

 

GitHub: https://github.com/Gausroth/HorizonWorldsTutorials.git Discord: https://discord.gg/hNyDQwdw4N YouTube: https://www.youtube.com/@GausRothTutorials

6 replies

gausroth
gausrothAnswer
Honored Contributor
March 26, 2025

If you want the script on the asset at spawn time. You will need to either create a copy of the asset with the script already attached or update the current asset after attaching the script. Let me see if I can get some screenshots.

EDIT: If you right click on the asset, click on Edit Template Definition then you should be able to add the script and save the change.

 

GitHub: https://github.com/Gausroth/HorizonWorldsTutorials.git Discord: https://discord.gg/hNyDQwdw4N YouTube: https://www.youtube.com/@GausRothTutorials
EchoExclusive
Inspiring
March 27, 2025

Hey, 

Thanks for the suggestions.

When I right click my asset (a 3d model) I see this "Place", "remove", "Replace", "Remove" but not edit template definition. 

EchoExclusive
Inspiring
March 27, 2025

Oh I think I figured out. I had to click edit template definition in the hierachy, not the Asset. My bad.

Also to create an asset I had to right click the asset in the Hierarchy and select "Create Asset". That was the part that lost me

 

Thank you for the help!!

dello32
Inspiring
March 27, 2025

I'm facing the same problem. I created an Asset with a simple text and shape, and placed my script in the root node (TagAsset):

Then I spawn this Asset using:
```
class TagManager extends hz.Component<typeof TagManager> {

  static propsDefinition = {
    quizManager: { type: hz.PropTypes.Entity },
    tagTemplate : { type: hz.PropTypes.Entity },
    tagAsset : { type: hz.PropTypes.Asset },
  };
  async start() {
    let spawner: hz.SpawnController | null = null;
    let spawnedEntity: hz.Entity;
    let textGizmo: hz.TextGizmo;
    spawner = new hz.SpawnController(this.props.tagAsset!,
      new hz.Vec3(0, 1.6, 0),
      hz.Quaternion.zero,
      hz.Vec3.one
    );
    await spawner.spawn();
    spawnedEntity = spawner.rootEntities.get()[0];
    textGizmo = spawnedEntity.children.get()[1] as hz.TextGizmo;
    console.log(textGizmo.name.get());
    console.log(`${textGizmo.text}`);
  }
}
hz.Component.register(TagManager);

```

It works, but it's impossible to interact with the asset’s script or even just change textGizmo.text.

The log prints:

tagText
undefined

=> An entity named tagAsset that is a TextGizmo but doesn’t have a .text attribute. That shouldn't be possible.

I tried waiting a few seconds before changing the text, but it still doesn’t work.

dello32
Inspiring
March 27, 2025

The solution:
bad: 
textGizmo
 = spawnedEntity.children.get()[1] as hz.TextGizmo;


good:
const child = spawnedEntity.children.get()[1];
const textGizmo = child.as(hz.TextGizmo);

Because:
as hz.TextGizmo : is a compilator cast, not a reel cast.
as(hz.TextGizmo) : is a true cast.

So, you can attach script to Asset, spawn Asset, make modification on this asset.