Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED]Get Currently Selected Button In C# Script Code?

Discussion in 'Scripting' started by FallenAngelSoftware, Jul 7, 2021.

  1. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Hi,

    How do we get the currently selected button in C# script code?

    Jesse
     
  2. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Hi,

    Below is the code:

    Jesse

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TitleScreenButtonLogic : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16. //        if () // Only do when this button is selected(not clicked on, just selected)
    17.         {
    18.             float buttonScreenY = this.transform.position.y;
    19.             Vector3 position = mainGlobals.ButtonSelectorLeftGO.transform.position;
    20.             position.y = buttonScreenY;
    21.             mainGlobals.ButtonSelectorLeftGO.transform.position = position;
    22.         }
    23.     }
    24. }
    25.  
     
  3. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class TitleScreenButtonLogic : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (this.gameObject == EventSystem.current.currentSelectedGameObject)
    18.         {
    19.             float buttonScreenY = this.transform.position.y;
    20.  
    21.             Vector3 leftPos = mainGlobals.ButtonSelectorLeftGO.transform.position;
    22.             leftPos.y = buttonScreenY;
    23.             mainGlobals.ButtonSelectorLeftGO.transform.position = leftPos;
    24.  
    25.             Vector3 rightPos = mainGlobals.ButtonSelectorRightGO.transform.position;
    26.             rightPos.y = buttonScreenY;
    27.             mainGlobals.ButtonSelectorRightGO.transform.position = rightPos;
    28.         }
    29.     }
    30. }
    31.