Jump to content
Unity Insider Forum

To select or highlight a button


PhantomPain

Recommended Posts

Hey everyone, sorry i am a total newby to scripting but I could manage to write a small script that when a character walks into a box colider, a UI appears and when he leaves that collider area, it disappears. Problem is: I want to highlight that button, so I can click it with keyboard and/or Controller. what command line do I need to include into my script? And where?
(It's not working to "just" use the EventSystem, because I will have multiple object with box colliders which triggers different buttons)

Here is my script btw.:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowUI : MonoBehaviour
{

    public GameObject uiObject;
    void Start()
    {
        uiObject.SetActive(false);
    }
    // Update is called once per frame
    void OnTriggerEnter(Collider player)
    {
        if (player.gameObject.tag == "Player")
        {
            uiObject.SetActive(true);
       
        }
    }

    void OnTriggerExit(Collider player)
    {
        if (player.gameObject.tag == "Player")
        {
            uiObject.SetActive(false);

        }
    }


}

Link zu diesem Kommentar
Auf anderen Seiten teilen

If you mean the button gets selected so you can instantly execute it with keyboard / controller then you would use 

EventSystem.current.SetSelectedGameObject( buttonGameObject );

buttonGameObject what the name already says the gameObject of the button what you want to highlight or have selected. 

Link zu diesem Kommentar
Auf anderen Seiten teilen

sorry I am pretty noobie on coding. Where exactly would I put this?

Like this:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowUI : MonoBehaviour
{

    public GameObject uiObject;
    void Start()
    {
        uiObject.SetActive(false);
    }
    // Update is called once per frame
    void OnTriggerEnter(Collider player)
    {
        if (player.gameObject.tag == "Player")
        {
            uiObject.SetActive(true);
       
        }
    }

    void OnTriggerExit(Collider player)
    {
        if (player.gameObject.tag == "Player")
        {
            uiObject.SetActive(false);

        }
    }
		{
		EventSystem.current.SetSelectedGameObject( buttonGameObject );
		}
} 

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

You should really look for c# tutorials. This is even basic thing which you learn in the first lessons. If you copy and paste you will never reach the goal because you would not understand what you are doing.

You put that under 

uiObject.SetActive(true);

And the buttonGameObject needs to be declared under public GameObject uiObject; and assigned via Inspector.

Please read or watch c# tutorials with unity before you start coding or copy pasting.

https://unity3d.com/de/learning-c-sharp-in-unity-for-beginners

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...