How to instantiate a prefab assigned from the inspector in Unity?

I just want to instantiate a prefab that is assigned from the inspector in Unity. Here is my code

import {AssetReference, Behaviour, GameObject, serializable, InstantiateOptions} from "@needle-tools/engine";
import {Object3D} from "three";

export class BallSpawner extends Behaviour {
    // @serializable(GameObject)
    // ball: GameObject | null = null;  

    ball? : GameObject;

    spawnPoint: Object3D | null = null;

    timer: number = 0;

    public start(): void {
        console.log("BallSpawner started");
        console.log("BallSpawner ball: " + this.ball);
        console.log("BallSpawner spawnPoint: " + this.spawnPoint);

    public update(): void {
        //instantiate the ball every 1 second
        this.timer += this.context.time.deltaTime;

        if (this.ball && this.spawnPoint && this.timer > 1) {
            console.log("BallSpawner update");
            GameObject.instantiate(this.ball) as GameObject;
            const spawnPoint = this.spawnPoint;
            this.timer = 0;

When I try ```ts
    ball: GameObject | null = null;   ```
I get the error:  ```Detected wrong usage of @serializable with Object3D or GameObject. Instead you should use AssetReference here! Please see the console for details.```

and when I try ```ts
    ball? : GameObject;```

i get the error:
```instance.clone is not a function```

[Original Post on Discord](

*by user 216692838757433347*

Hey :wave: AssetReference works similar to Addresables in Unity, in a sense that you need a special way how to laod and instatiate the object since it is not loaded yet.

There is a super simple API for that:

Mind that it is asynchronous and that it returns a callback when it loads and spawns, you can also await that call.

Full example:


You can always search the docs for a specific keyword :+1: :cactus:

Regarding the code please note this difference - AssetReference and GameObject are separate things, the type in @serializable and on the property should match.

    // for prefab references - will be loaded dynamically with instantiate()
    ball? : AssetReference;

    // for scene references - loaded at start already
    ball? : GameObject;

Thank you for the quick responses! Iā€™ll look through the questions and documentation to figure how to get the instantiated object to interact with the physics system

by user 216692838757433347

It should just do! No changes needed if that object has collider/rigidbody