When I duplicate my UI, navigation stops working
Hello,
So, I am building an environment with NPC's, every time I approach one of those characters and press a key I need UI to pop up in order to interact with them.
That works with one character but if I try to duplicate it and add it to another character the UI on both characters stop working. Any idea why or how to fix this?
I am essentially using a trigger that when I am colliding and I press a key I bring up buttons. I think the issue might be the event system but I am not sure. Please help!!
Answer by ogotera · Mar 09, 2020 at 05:40 PM
I was able to figure it out. What I did was adding this line of code when I am not colliding with the NPC:
EventSystem.current.UpdateModules();
Answer by streeetwalker · Mar 08, 2020 at 10:23 AM
Don't duplicate the event system - you only need one of those. Aside from that, how are you duplicating the UI, and what is interactive within the UI - for example, you have buttons? If so, then how are you handling that interactivity?
It depends on how you set up the interactive elements. If now you make a copy of them, yes, you may need some way to accommodate the new interactivity.
To help you further we need to see a more detailed view of how the UI is set up on an object, and the scripts that you have that get the interaction and the scripts controls or reacts to the interaction.
If I were attempting this, I would attach the Canvas as a child to the npc. I would have an event script on each button that would either send unique messages that a controller script on the npc would listen for, or the button would directly reference the controller script. That way when I duplicated the npc, it would have it's own self contained UI system.
However, if the message / listener route would be more complex, because you'd have to ensure that a UI button click on NPC A was not received by the controller on NPC B. So with the message you'd need to send some kind of identifier with the message, and the controller script would parse out which NPC was reacting.
using these kinds of scheme, you shold be able to make as many copies of the NPC as you need.
Thanks for your response!
What I am doing is I have a script that switches canvas based on the ui that I needs to appear. So, for example I have a canvas with two buttons that when you approach a character says "Interact" and "Play" using buttons.
If you click interact, then it goes to another canvas that has other buttons with options "send message" and "animation". And so on. So I am hiding/showing canvas depending on what button the user selects
Now I want to put that same ui in another character but when I try duplicatin the set of canvas now the buttons aren't highlighted anymore so I can't interact with the UI.
Here is my script for switch canvas:
public class SwitchCanvas : $$anonymous$$onoBehaviour {
public GameObject offCanvas;
public GameObject OnCanvas;
public GameObject firstUI;
public void Switch()
{
offCanvas.SetActive(true);
OnCanvas.SetActive(false);
GameObject.Find("EventSystem").GetComponent<EventSystem>().SetSelectedGameObject(firstUI, null);
}
}
Am I doing it wrong? Thanks again!
I don't know. It sounds complicated. It's impossible to tell by looking at this where it could be going wrong. I'd start sticking debug statements into your code to try to pin down exactly where it fails.
Also, there doesn't seem to be any errors. It just stops working which is weird...