Open / close canvas, on click from different 3d objects, and switches if necessary

Hi all,

I am sorry if this a stupid request but I think I am not understanding how to do this thing gracefully

I have several 3d objects in my scene, like characters. They are at different part of the scene.

When you click on them I want to have a pop-up canvas showing on top of them , showing their stats, health, etc.

The canvas are a prefab. And a script takes some parameters from the object clicked on in order to show the right information.

When you click, for the first time, on a 3d objects of course it shows you (setActive(true)) the panel. But then, if you click on a new one, it closes (setActive(false)) the other one, and open the new one .

This is where I am stuck.

To code the fact that :

  • If a canvas is already open , close it first, then open the new one. How to find which canvas is open ? Search by object name ? Or should I tell to deactivate all.
  • if you click on the same object, do nothing (as the panel is already open)

Maybe I should use a switch statement ?

Should I put all my pop-up UI canvas in a Pop-upmanager script, use a switch statement in an function that waits for a click function coming from the 3d objects scripts…???

Thank you a lot ! Hope it is clear !

Is the data similar enough to be presented by the same popup? I would just have one panel on one canvas and change the data around, hiding and showing the (one) popup as appropriate.

No need to close it unless you are animating it popping in and out. You could just have a init method, set it’s position, text ect when calling the init method and enable it if it is not already active.

Yes similar data

I think I have a prefab, I just instantiate the panel for each unit when selected…

But the position is different ( hovering on each 3d objects)…

But the thing is that , a normal state of the game is without any panels showing on top of these 3d items… So they have to close at some point …

My issue is knowing each last panel is still open (if any) and closing it before opening the new one somewhere else

Yes they are popping out

Think of it like , on a board game, if I select a unit, then I have this hovering info panel showing up on top of the unit…

And

The thing is that , a normal state of the game is without any panels showing on top of these 3d items… So they have to close at some point …

Why don’t you just move the panel to match the selected item’s position and only keep track of one? Having a new tooltip panel for every single possible interactable item is way overkill.

If you absolutely have to though, just add a static reference to your controller.

class PopupController : MonoBehaviour
{
   private static PopupController lastSelected = null;

   public void OnShow()
   {
      // hide the old one:
      if(lastSelected != null)
      {
         lastSelected.Hide();
      }

     // set this one as the last selected
      lastSelected = this;
   }
}