Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Auto Select on Highlight

Discussion in 'Editor & General Support' started by jakebaker865, Jul 14, 2020.

  1. jakebaker865

    jakebaker865

    Joined:
    May 6, 2020
    Posts:
    36
    Hello! Thank you in advance.

    I am having some issues with a character selection system. I need to be able to automatically have the character switch when a button is highlighted. Right now, I have three buttons that come up when a key is pressed and go away when the key is let go. You can select a character and then press enter for the character to change. I want to eliminate the "press enter" section of that, so that you can highlight the character and they automatically switch to the highlighted character name. I've tried to Invoke the OnClick event, but that doesn't seem to work.

    I'm really lost on how to do this. Any help is appreciated.

    Code (CSharp):
    1. public GameObject characterSelect;
    2.  
    3.     public GameObject knight;
    4.     public GameObject viking;
    5.     public GameObject pike;
    6.  
    7.     public GameObject knightCam;
    8.     public GameObject vikingCam;
    9.     public GameObject pikeCam;
    10.  
    11.     public GameObject currentChar;
    12.  
    13.     public CharacterController2D knightController;
    14.     public CharacterController2D vikingController;
    15.     public CharacterController2D pikeController;
    16.  
    17.     public Button knightButton;
    18.     public Button vikingButton;
    19.     public Button pikeButton;
    20.  
    21.     void Update()
    22.     {
    23.  
    24.  
    25.         if (Input.GetButtonDown("CharacterSelect"))
    26.         {
    27.             characterSelect.SetActive(true);
    28.             Time.timeScale = 0.02f;
    29.             knightController.canMove = false;
    30.             vikingController.canMove = false;
    31.             pikeController.canMove = false;
    32.         }
    33.         else if (Input.GetButtonUp("CharacterSelect"))
    34.         {
    35.             characterSelect.SetActive(false);
    36.             Time.timeScale = 1f;
    37.             knightController.canMove = true;
    38.             vikingController.canMove = true;
    39.             pikeController.canMove = true;
    40.  
    41.         }
    42.      
    43.  
    44.     }
    45.  
    46.     public void KnightActive()
    47.     {
    48.         knightButton.onClick.Invoke();
    49.         knight.SetActive(true);
    50.             knightCam.SetActive(true);
    51.             viking.SetActive(false);
    52.             vikingCam.SetActive(false);
    53.             pike.SetActive(false);
    54.             pikeCam.SetActive(false);
    55.         knight.transform.position = currentChar.transform.position;
    56.         currentChar = knight;
    57.        
    58.     }
    59.  
    60.     public void VikingActive()
    61.     {
    62.         vikingButton.onClick.Invoke();
    63.         knight.SetActive(false);
    64.             knightCam.SetActive(false);
    65.             viking.SetActive(true);
    66.             vikingCam.SetActive(true);
    67.             pike.SetActive(false);
    68.             pikeCam.SetActive(false);
    69.         viking.transform.position = currentChar.transform.position;
    70.         currentChar = viking;
    71.  
    72.     }
    73.  
    74.     public void PikeActive()
    75.     {
    76.         pikeButton.onClick.Invoke();
    77.         knight.SetActive(false);
    78.             knightCam.SetActive(false);
    79.             viking.SetActive(false);
    80.             vikingCam.SetActive(false);
    81.             pike.SetActive(true);
    82.             pikeCam.SetActive(true);
    83.         pike.transform.position = currentChar.transform.position;
    84.         currentChar = pike;
    85.  
    86.     }