I have created some basic cube and sphere prefabs and have referenced them in Unity editor. (when they are placed in the scene directly, they always render)
Sometimes the prefab spawns, sometimes not. I am always saving Unity and entering play mode before each script edit.
Only 2 prefabs max will appear on screen even though more are being instantiated
Eventually Unity starts hanging and crashes (see screenshot)
Hello there ā we have an experimental AI bot that might be able to help you with your question. Would you like to try this out?
How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.
The behavior youāre observing with inconsistent prefab instantiation and the subsequent crash of Unity might be related to the way youāre instantiating the objects asynchronously without waiting for each instantiation to complete before starting another. Since the start method in a behavior runs asynchronously, itās important to ensure each asynchronous operation completes before moving onto the next, especially if these operations can have side effects like creating new objects in the scene.
Letās modify your start method to wait for each instantiation to finish before proceeding to the next one:
import { AssetReference, Behaviour, InstantiateOptions, serializable, Vector3 } from "@needle-tools/engine";
export class InstantiateTest2 extends Behaviour {
@serializable(AssetReference)
myObjectReference: AssetReference | null = null;
@serializable(AssetReference)
myObjectReference2: AssetReference | null = null;
@serializable(AssetReference)
myObjectReference3: AssetReference | null = null;
async start() {
const opts = new InstantiateOptions();
opts.position = this.worldPosition;
if (this.myObjectReference) {
await this.myObjectReference.instantiate(opts);
}
opts.position = new Vector3(5, opts.position.y, opts.position.z);
if (this.myObjectReference2) {
await this.myObjectReference2.instantiate(opts);
}
opts.position = new Vector3(10, opts.position.y, opts.position.z);
if (this.myObjectReference3) {
await this.myObjectReference3.instantiate(opts);
}
}
}
I understand, letās check the instantiation process, and define the position before each instantiation. We will also perform error checks to ensure the positions are not undefined:
import { AssetReference, Behaviour, InstantiateOptions, serializable, Vector3 } from "@needle-tools/engine";
export class InstantiateTest2 extends Behaviour {
@serializable(AssetReference)
myObjectReference: AssetReference | null = null;
@serializable(AssetReference)
myObjectReference2: AssetReference | null = null;
@serializable(AssetReference)
myObjectReference3: AssetReference | null = null;
async start() {
if (!this.myObjectReference || !this.myObjectReference2 || !this.myObjectReference3) {
console.error("One or more AssetReferences are not set.");
return;
}
const opts = new InstantiateOptions();
opts.position = this.worldPosition;
const newObj = await this.myObjectReference.instantiate(opts);
if (newObj) {
opts.position = new Vector3(5, opts.position.y, opts.position.z);
await this.myObjectReference2.instantiate(opts);
opts.position = new Vector3(10, opts.position.y, opts.position.z);
await this.myObjectReference3.instantiate(opts);
} else {
console.error("Error instantiating the first object.");
}
}
}
This code now includes error checking to ensure that the AssetReferences are not null or undefined before attempting to instantiate objects. It also includes a check for the newly instantiated object to ensure that the second and third instantiations only occur if the first instantiation was successful. This should help prevent errors related to undefined positions and improve error handling during the instantiation process.
0 messages leftGPT 3.5 thread_k1JRBxb4YqkhTGYGwufEebZl Thanks for your feedback!
Hi, the reason is that the async callback for instantiate is not awaited and the Instantiate options that are passed in is immediately modified. You can fix this by creating new instantiate options per āinstantiateā call.
I think it might be better if internally the options would be copied (to make your code above work as well) - but right now thatās how you have to do it
@BOTBOT in case you didnt see it: I changed that addressable instantiate behaviour to copy the passed in options immediatly to make your life easer and also backported it to the latest stable version Discord
I think itās worked - however when moving the parent GameObject within Unity (with Needle liveSync), the children donāt move as well (like typical parent - child relations)