Hi!
I’m trying to turn off and on a canvas Image. What is the correct way to do it?
So far I tried the usual “Unity” like methods with no success:
import { GameObject } from "@needle-tools/engine";
export class Example extends Behaviour{
@serializable(GameObject)
triggerImage?: GameObject;
start(): void {
//This will not work
this.triggerImage.transform.visible = false;
//Also in this particular case should be using rectTransformInstead
const rect = this.triggerImage.getComponent(RectTransform);
if(rect) {
rect.enabled = false; //As expected, no changes.
}
}
}
Also tried accessing it through the Graphic class:
import { Image, Graphic } from "@needle-tools/engine";
export class Example extends Behaviour{
@serializable(Image)
triggerImage?: Image;
start(): void {
// Inheritance tree goes like this: Image -> MaskableGraphic -> Graphic
const maskable = this.triggerImage as Graphic;
maskable.setAlphaFactor(1); // Apparently this doesnt make the trick.
// Directly disabling the component also doesnt do anything.
//this.triggerImage.enabled = false;
}
}
I never disabled the rect transform component in Unity - I dont think that works there as well (disabling a transform) - or is this something special to RectTransform that I just didnt know?
via Unity (see screenshot) via a event - in typescript you could just set gameObject.visible = false for example (or call GameObject.setActive(gameObject, false))
the reference to the image was to an instanced reference, so when i printed out the result, it printed undefined, but if i unpack the prefab GameObject.setActive(this.triggerImage, false) works as expected
InstancedReference: you mean you had a scene reference from a Prefab asset in Unity? Can you show some screenshots in Unity on what you mean and what references what?
Prefabs in the scene are not prefabs at runtime or in the export. Only if they’re assets and referenced from a component or if you add a GltfObject to some part of your hierarchy - otherwise everything in the scene hierarchy will be “unpacked” on export technically
Ahh yes: Here first Tooltip is an unpacked prefab, while Tooltip2 is a prefab that i dropped to the scene, structure wise both are the same.
In the image I’m selecting the prefab version and as you can see, tooltipTrigger should be assigned to it’s child element.