custom script loses it’s reference in runtime, even though it is referenced through the inspector in untiy
I have this script
import { Behaviour, serializable, Button, GameObject, Text, Image, Sprite} from "@needle-tools/engine";
import {foodScriptableObjectButWorksBest} from "./foodScriptableObjectButWorksBest";
import {ARFoodVisulizerButWorks} from "./ARFoodVisulizerButWorks";
// Documentation → https://docs.needle.tools/scripting
export class buttonButWorks extends Behaviour {
@serializable(foodScriptableObjectButWorksBest)
foodScript!: foodScriptableObjectButWorksBest;
@serializable(ARFoodVisulizerButWorks)
ARFoodVisulizer?: ARFoodVisulizerButWorks;
ARMode()
{
if (this.ARFoodVisulizer)
{
this.ARFoodVisulizer.prefabToPlace = this.foodScript.threeDModdle!;
}
else
{
console.warn("Missing reference ARFoodVisulizer-");
}
}
}
ARFoodVisulizer is being undifned as “Missing reference ARFoodVisulizer-” is being warned
the script is longer but i reduced it to make reading it easier
through a button I call ARMode function and then it gives me that warning
the refernce ARFoodVisulizer is set in the inspector, even tried making a differnt variable to see if that one was bugged and also tried keeping the object always set active but that did nothing
I am using untiy 6 and the latest version of needle
Hi h-dragon,
This issue usually occurs when the serialized type (in your case ARFoodVisulizerButWorks) doesn’t exactly match the instance attached—or when changes in the script (like renames or moving files) break the inspector linkage. Even though the reference appears to be set in the inspector, at runtime Unity (and Needle Engine’s serialization system) may not be able to match it again if something changed. Here are a few steps you can take:
• Verify that the ARFoodVisulizerButWorks script is compiled correctly and hasn’t been renamed or moved. If you’ve made changes, remove and reassign the component in the inspector.
• Make sure that the GameObject holding ARFoodVisulizer is active during runtime and isn’t inadvertently destroyed or deactivated.
• Add a log in your start method to check if the reference is properly assigned early in the component lifecycle.
• Confirm that no other script clears or reinitializes the reference.
Below is an updated version of your code with logging in start():
import { Behaviour, serializable } from "@needle-tools/engine";
import { foodScriptableObjectButWorksBest } from "./foodScriptableObjectButWorksBest";
import { ARFoodVisulizerButWorks } from "./ARFoodVisulizerButWorks";
export class buttonButWorks extends Behaviour {
@serializable(foodScriptableObjectButWorksBest)
foodScript!: foodScriptableObjectButWorksBest;
@serializable(ARFoodVisulizerButWorks)
ARFoodVisulizer?: ARFoodVisulizerButWorks;
start() {
// Log the ARFoodVisulizer reference to ensure it is assigned during initialization
console.log("ARFoodVisulizer reference:", this.ARFoodVisulizer);
}
ARMode() {
if (this.ARFoodVisulizer) {
this.ARFoodVisulizer.prefabToPlace = this.foodScript.threeDModdle!;
} else {
console.warn("Missing reference ARFoodVisulizer-");
}
}
}
By logging the reference in start(), you can check whether it’s available immediately or if something (like a scene reload or script reinitialization) is causing it to lose its value. If the log shows null or undefined, try reassigning it in the inspector (after any recent changes) and make sure the component is part of an active GameObject.
For more details on how Needle Engine handles serialization and inspector references, please consult the documentation at Creating and using Components | Needle Engine Documentation.
Hope this helps!
1 replies left - Become PRO for more replies.
thread_w8OQo9vZd9DDZlTdv375rSOK
Reply to this post directly if you need further help (bottom right button)
start() {
// Log the ARFoodVisulizer reference to ensure it is assigned during initialization
console.log("ARFoodVisulizer reference:", this.ARFoodVisulizer);
}
yeah that also is not gettign the refernce
hey marwie thanks for the quick response again
1)it’s being called when a button on another game object is being pressed
import { Behaviour, serializable, Button} from "@needle-tools/engine";
import { buttonButWorks } from "./buttonButWorks";
// Documentation → https://docs.needle.tools/scripting
export class buttonOnClickEvent extends Behaviour {
@serializable(Button)
uiButton!: Button;
@serializable(buttonButWorks)
targetScript!: buttonButWorks;
start() {
if (this.uiButton && this.targetScript) {
this.uiButton.onClick.addEventListener(() => {
this.targetScript.ARMode();
});
} else {
console.warn("Please assign all references in the Inspector. ?");
}
}
}
- I 'm not sure what you mean by type but here’s the script for it
import { Behaviour, GameObject, serializable, Camera, AssetReference} from "@needle-tools/engine";
import {instantiate, Physics, Input } from "@needle-tools/engine";
import { InstantiateOptions } from "@needle-tools/engine";
import { Vector3, Vector2} from "three";
import { Object3D } from "three";
export class ARFoodVisulizerButWorks extends Behaviour {
@serializable(AssetReference)
prefabToPlace!: AssetReference;
//@serializable(Camera)
//arCamera?: Camera;
start() {
}
update() {
// Detect tap or click
//const inputPos = Input.touchCount > 0
// ? Input.getTouch(0).position
// : Input.mousePosition;
// const screenPos: Vector2 = Input.mousePosition;
// Raycast from camera
//const ray = this.arCamera?.screenPointToRay(inputPos);
//if (!ray) return;
const pos = this.context.input.mousePosition;
if (!this.context.input.mouseDown) return; // for click
this.context.input.convertScreenspaceToRaycastSpace(pos);
const hit = this.context.physics.engine?.raycast(pos);
if (hit && this.prefabToPlace) {
const options = new InstantiateOptions();
options.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
const obj = this.prefabToPlace.instantiate(options);
}
}
}
the purpose of that script is just to spawn a food item where the player is pressing or tapping but I’m stuck on the part where I give it the prefabToPlace from the button but works script, because it’s losing it’s refernce to this script
- only some web ar game objects unrelated to this
on an unrelated note, if a gmaeobject has a parent with this tag while it doesn’t will it also coutn as it having it
Can you show the setup in Unity with the components and how they’re conected?
If a parent object is set to EditorOnly it will not export the object NOR any of it’s children. So if your ARFoodVisulizerButWorks
is in the child hierarchy (a parent is set to EditorOnly) then it won’t be exported. This would explain the missing reference
yeah that editor tag thing has just saved me from a future bug, thanks
but this issue still remains
here’s the setup https://youtu.be/TXXxfV54pY8
Please send a minimal bugreport with this (with just the relevant part and necessary components and anything else removed from the scene)