Turning Image component on and off

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;
  }
}

Original Post on Discord

by user 368204675922657280

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

Hi, this means if you disable the component the image in the web stays visible? That sounds like a bug / would be a bug

this works for me (setting enabled to false via a button) - can you check that you asctually got the image component?

if ( !rect ) console.error("No image component found")

.
20231221-171849_Collaborative_Sandbox_-_Google_Chrome-logo.mp4

I’ll check it out

by user 368204675922657280

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?

Disabling the whole image object should also work - depending on what you want

via Unity (see screenshot) via a event - in typescript you could just set gameObject.visible = false for example (or call GameObject.setActive(gameObject, false))
image.png

Yes i got no errors regarding the rect component or the Image reference.
I’ll Update the Engine version just for in case, I’m in v3.20.3.

by user 368204675922657280

Ahhhh I found an interesting thing, the problem is that I’m using prefabs

by user 368204675922657280

mmmh ok I doubt it’s the version tbh - but worth a try maybe

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

by user 368204675922657280

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.

by user 368204675922657280

At least in theory

by user 368204675922657280

Do you think you could create a minimal repro and send it as a bugreport?

Sure, i’ll test in a new project, if it continues to behave the same i’ll send it as a bugreport

by user 368204675922657280