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;  

    @serializable(AssetReference)
    ball? : GameObject;

    @serializable(Object3D) 
    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;
            
            //newBall.position.copy(spawnPoint.position);
            this.timer = 0;
        }
    }
}```

When I try ```ts
    @serializable(GameObject)
    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
    @serializable(AssetReference)
    ball? : GameObject;```

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

[Original Post on Discord](https://discord.com/channels/717429793926283276/1154747892867809360)

*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:
this.ball?.instantiate()

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

Full example: https://engine.needle.tools/docs/scripting-examples.html#reference-and-load-an-asset-from-unity-prefab-or-sceneasset

----

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

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()
    @serializable(AssetReference)
    ball? : AssetReference;

    // for scene references - loaded at start already
    @serializable(GameObject)
    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