How to respond to gameObject.visible changes?

My problem is, that I want to react on gameObject.visible not in Unity itself, but back in typeScript.
Let me explain. We have typeScript that toggles some UI Elements in the threejs canvas via this.uiPanels[i].gameObject.visible = false; But we want to toggle some HTML elements in the sourrounding website as well*, so I made a TS script to try to toggle the HTML Elements via CSSSelector.

    htmlSelector: string = "#btn-hide-cube";
    private element? : HTMLElement;

    onEnable() : void { this.showElement(true); }
    start(): void { this.showElement(true); }
    onDisable() : void{ this.showElement(false); }
    onDestroy(): void { this.showElement(true); }

    private showElement(state: boolean) {
        if (!this.element)
          this.element = document.querySelector(this.htmlSelector) as HTMLElement;
        if (this.element) {
            if (state)
       = "visible";
       = "hidden";

But aside of start() no other function will ever be called when gameObject.visible changes. So the question is, how can I respond to gameObject.visible?

Original Post on Discord

by user 689743366355025931

Hi, can you share some more info about your hierarchy or how/where you create the objects? Is some parent object perhaps already disabled (or disabled at the same time?) - it doesnt reproduce in a simple example

Does your question mean, that gameObject.visible changes are calling the unity lifecycle functions?

by user 689743366355025931

That there is a hierarchy and disabling a parent will disable the children itself as well resulting in no needle events being fired. Your issue might be caused by a disabled parent.

Can you show more of your hiearchy and what scripts you have there?
Can you replicate this behavior if you duplicate your scene and remove objects with other logic?

But not reciving any calls besides on start is indeed odd.

Sorry, I can not share the project. And I think parent objects in the hierachy are not the problem. But I’am very new to needle, so maybe I miss something. What works for now is, that enableling the HTML object via gameObject.visible = true; from a Unity function. Then, I hide the the object via a TS function that do = "hidden";. And then the exact same function that shows the HTML object on start does not show it right now. The HTML object still have visibillity: hidden. Do I have to change the CSS visibillity by my self? My expectation is that gameObject.visible = true does exactly this.

by user 689743366355025931

When you send a bugreport we can look at your scene and the files will only be shared internally with our team for debugging (via the Needle Engine/Report a bug menu item) - upload is encrypted

Ok, I managed to hack a project together, based on your minimal (Needle) scene template. Here you can find it.
Blue button show the panel, red button hides. So far so good. But if the close button inside of the panel was clicked, the blue button dont show the panel again. But after the red button disables the gameObject, the blue button can activate em again.

by user 689743366355025931

Hi, as far as I can see the button inside the HTML element just sets the HTML element to hidden

When you click the blue button it sets “visible” to true for an object that is already/still visible (the RectTransform’s gameobject in the scene) - this will not cause onEnable or any other method to be called because it’s still enabled

what you might want to do is to call the showElement method from your blue button to set the html element to visible again


I hope that makes sense. You created the HTML (cyan) panel in index.html and there’s no magic link to objects in the three.js scene (or in the component hierarchy) unfortunately

Let me know if that helps you with the problem

Yeah, that was my best guess yesterday too, that I just set the CSS visibility attribute, but it doesn’t change anything in the Needle scene (aka Unity).

Your method of simply using the showElement() function is unfortunately not possible. Sure in this super simple scene it makes total sense but in our real project everything is much more complicated. I will try to make it work somehow. But it would be easier if I could get gameObject.visible to switch the visibillity to visible in any case no matter how it was set in the hierarchy before. Because there is only this one place in the code that I would have to change.

For this reason, I’ve already tried to deactivate it in any case first and then activate it again:

  panel.gameObject.visible = false;
  panel.gameObject.activeSelf = false;
  panel.gameObject.visible = true;
  panel.gameObject.activeSelf = true;

But that had not worked. With it also no lifecycle methods were executed.

by user 689743366355025931