#Event System First Selected

1 messages Β· Page 1 of 1 (latest)

golden pebble
#

If i'm not mistaken, the EventSystem.FirstSelect just work once, when the game starts. But if you need to change the selected gameObject on script, you can use the method EventSystem.SetSelectedGameObject() (docs: https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.EventSystem.SetSelectedGameObject.html) or you can use the methods Selectable.Select (docs : https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Selectable.Select.html), in this case, classes like Button, InputField, Slider, are all Selectable, so you can call the method Select from them

#

If you have any question feel free to ask

dreamy lake
#

First of all @golden pebble thank you so much for your time to answer me. Second, I already tried and now the button is selected with the function EventSystem.SetSelectedGameObject().
Unfortunatelly, I can't navigate inside the menu. When I access the first button is highlighted but I couldn't navigate inside the menu and I'm a little bit lost about what I have to do right now...

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class eventSystemFirstSelected : MonoBehaviour
{
[SerializeField] private GameObject pauseInterface;
[SerializeField] private GameObject deathInterface;
private pauseMenu pauseScript;
private deathMenu deathScript;

public void Start()
{
    pauseScript = FindObjectOfType<pauseMenu>();
    deathScript = FindObjectOfType<deathMenu>();
}

private void Update()
{
    if (pauseScript.gamePaused)
    {
        EventSystem.current.SetSelectedGameObject(pauseInterface);
    } 
    else if (deathScript.playerDeath)
    {
        EventSystem.current.SetSelectedGameObject(deathInterface);
    }
}

}`

golden pebble
#

You're welcome @dreamy lake. I think i know why the navigation isn't working anymore. This script calls the SetSelectedGameObject on Update() method, so always when pauseScript.gamePause is equals to true, it will select the pauseInterface. Then if you select a game object from navigation, the script will select the pauseInterface again, making impossible to select any other object. I think a easy solution for this would be call SetSelectedGameObject only once: when the player opens the menu.

#

To do this, you could create a Monobehaviour class called like Menu, that have a GameObject variable that stores which gameObject must be selected when you start the menu. If the menu starts when its GameObject is actived, you could add the SetSelectedGameObject inside the method OnEnable(), which is called when a component activates. The docs of OnEnable is available at: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html

#

If you have any question, let me know

dreamy lake
#

Sorry @golden pebble but I didn't understand you. I have, in this moment, two scripts for the pause menu and for the death menu. Do you mean that? And I'm not sure about what do you mean...

lament stumpBOT
#

An image of your code is not helpful

When asking a question about a problem with code, people who are volunteering to help need the text of the code. Unless you are asking about your IDE - and not the code itself - images of the code are not an acceptable substitute.
Source: https://idownvotedbecau.se/imageofcode

Please send your code as a codeblock. If you don't know how to send a codeblock, type []cb

golden pebble
# dreamy lake Sorry <@254648781079838731> but I didn't understand you. I have, in this moment,...

Don't worry, i'll try to explain better. First starting by the problem. On your script eventSystemFirstSelected, the Update() method says that if the game is paused, the pauseInterface must be selected. The problem is that piece of code is on *Update * method. This method is called once each frame. So let's say that on frame 0 the player paused the game. Then, the script will select the pauseInterface. No problems yet. But let's say that on frame 10, the player decide to navigate on the menu and select the second button. So this happens with no problems, however, on frame the next frame (11), the *Update *of eventSystemFirstSelected will be executed, so it will check if the game is paused, and since it is, it will select again the pauseInterface, "overwriting" the player input. Since this is meansured in frames (and usually 60 of them are executed per second), we can't see the player selecting the button of menu, and a some time later, the script reselecting the pauseInterface, so it seems that the navigation just stopped to work.

#

Do you have some question about why the problem is happening?

dreamy lake
#

No, your explanation is perfect. Like a real good teacher πŸ˜„

golden pebble
#

Thank you very much! πŸ˜„

#

So, now that we understood the problem, we can get in the solution. Since the problems is the game always select the pauseInterface, we need a way to select it just once. In order to do that, we have to know what starts the pause menu, in this case, is the player input. So, the line that select pauseInterface should be between the lines of codes that initialise the pauseMenu

#

The same logic applies for the player death interface, you have to check where the menu is initialised, and add the select logic there

#

Do you have some question about how this solution works?

dreamy lake
#

Noup. I understood everything. Those days I'm going to try it. If I have some questions I'll let you know. Thank you so much @golden pebble πŸ˜„

golden pebble
#

You're quite welcomeπŸ˜„

dreamy lake
#

Hello there @golden pebble,
I hope all goes well. I'm a little bit stucked with the code. I put all the code together in only one script. And this is the result:

#

The problem is that the pause select the first button, the death menu too but I can't navigate it

#

Would you need something else to check what is happened here?

golden pebble
#

Hello @dreamy lake, is the problem happening only with the death screen or also with the pause menu? If is just with the death screen, the problem still the same, because the method Death() is called always when lifesCounterFromThisSide.lifesCounter <= 0 is true. And since once the player dies, it is always true, it still calling Death() every frame. But it the pause also is not working, either it still calling Pause() each frame and we didn't realized or there is another thing preventing the navigation

dreamy lake
#

Tomorrow I'll recording my screen to show you what is going on. Thank you so much for your time!

golden pebble
#

Alright. You're quite welcome!

dreamy lake
#

Sorry @golden pebble, yesterday was a bussy day. I'm going to send you the video right now πŸ™‚

#

If you can see when I press the pause button it makes everything as I want but, when I die it looks like the eventSystem it's not 100% working... Maybe the problem is with the canvas and not with the code? If you need anything else just let me know

golden pebble
#

Don't worry
I think the line that selects the first button still beeing called once per frame. The line is on Death() function, and it is called always when the condition lifesCounterFromThisSide.lifesCounter <= 0 is true, so if the lifesCounter still <= than 0, it makes the code select the first button every frame, like the bug with pause. But to make sure, could you add a Debug.Log on Death void please?

dreamy lake
#

I'm not sure what I have to put inside the Debug.Log so I put an string with the word "Enter" to make sure that it's entering and lifesCounterFromThisSide.lifesCounter. I'm going to send you the video

#

You will notice that the color about the Death Screen first selected changed. Now has this color because I remove the fade duration of the button.
So I see that we are inside the function but we can navigate there.

golden pebble
#

And yeah, it is beeing called once per frame

#

Do you have some question about why this problem is happening?

dreamy lake
#

I can't navigate because it's always calling the function in each frame

#

I guess I have to change the way that I calling the Death function

golden pebble
#

Yeah, that's right

#

Do you want help to figure out some way to do that?

dreamy lake
#

I would love to. When I made the menu script (for the title and the other things) I find the way to do everything without the Update doing this:

#

I have to implement a few things like to come back only pressing a button or a key and not with a screen button

#

And solve the StoryFunctionInside(). I tried to do the same but it doesn't work

#

I mean with the Death() creating an invisible button that is always selected but it doesn't work too

golden pebble
#

Got it. I have an idea that is similar to StoryFunctionInside(). Instead of use a new bool variable, you can try to use the function GameObject.activelSelf to check if the death screen is actived, then you only load the DeathScreen if its gameobject isn't actived yet

dreamy lake
#

GameObject.activelSelf inside the Update?

golden pebble
#

Yeah, inside the if that calls the Death function

dreamy lake
#

Let me try, 1 minute

#

Like this:

if (lifesCounterFromThisSide.lifesCounter <= 0) { deathInterface.SetActive(!pauseInterface.activeSelf); Death(); }

Doesn't work

golden pebble
#

No, was like if(lifesCounterFromThisSide.lifesCounter <= 0 && !deathInterface.activeSelf)

dreamy lake
#

FUCK

#

IT WORKS

golden pebble
golden pebble
dreamy lake
#

Sounds fantastic

golden pebble
#

Have you understood why this works?

golden pebble
dreamy lake
#

Igor, what can I do for you to return the favor?

golden pebble
#

You don't need to do anything, thanks

dreamy lake
#

Are you a game developer too?

golden pebble
#

Yes, i am

dreamy lake
#

I'm a programmer but one of my pasions it's to be a sound designer. If you need something with the sound let me know and I'll help you

golden pebble
#

Ok, sounds good. Thank you very much

dreamy lake
#

Create sounds, find the best sounds, mixing something in your game, reverbs, anything

#

No please, thank you to you. Without your help maybe I'll never find the solution. Thank you. You real name it's Igor?

golden pebble
#

You're quite welcome. Yeah, my name is Igor

#

What about you?

dreamy lake
#

I would like to include you on the credits for your help. It's not a real game, it's only a demo, but your help was so important for me

#

Sergi, it's a catalan name, from Spain

golden pebble
dreamy lake
#

I have some brazilian friends. Which city or town? Maybe I know it

#

I'll send you the demo when it's finished

golden pebble
dreamy lake
#

Of course, privacy it's so important πŸ™‚

golden pebble
dreamy lake
#

And sorry if I bother you with my question

golden pebble
#

Don't worry

#

Is because here is public server

dreamy lake
#

True, smart movement

#

Well Igor, I just send you a friends request and let me know if you need anything else that I can help you

#

Thank you so much for you knowledge πŸ˜„

golden pebble
golden pebble