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…???
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.
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;
}
}