Not certain this is the correct place for this, so apologies if not!
I’m having a strange interaction with (I think?) the Input System (I’m using 1.1.1) and the EventSystem where I’m getting the button pressed in my code via ‘EventSystem.current.currentSelectedGameObject.GetComponent()’ which works 100% fine, untill I’m walking backwards (S key) aswell ingame?
If I click the button while walking backwards (holding S) the button clicked returns as ‘ResetDefault’ rather than the actual button clicked - and I can’t figure out why hold ‘S’ would make any difference to this at all? Or how to fix it!
Appreciate any help, code for the class in question below.
UpdateActive is called by the button on-click, and the issue is (I guess) at line 73 where I get the button but it doesnt get the button while walking backwards for whatever reason?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class KeybindTab : MonoBehaviour
{
private Button currentlyActive;
private float activeHeight;
private float inactiveHeight;
private RectTransform tabs;
private RectTransform bodies;
public void Awake()
{
tabs = this.transform.Find("Tabs").GetComponent<RectTransform>();
bodies = this.transform.Find("Bodies").GetComponent<RectTransform>();
}
public void Start()
{
activeHeight = tabs.rect.height - UISettings.padding;
inactiveHeight = tabs.rect.height - ((2*UISettings.padding) + 1);
ColorBlock colorBlock;
// Get currently active on start-up
for(int loop = 0; loop < tabs.childCount; loop++)
{
colorBlock = tabs.GetChild(loop).gameObject.GetComponent<Button>().colors;
// loop through all tabs, find one colored as active
if(colorBlock.normalColor == UISettings.activeColor)
{
currentlyActive = tabs.GetChild(loop).gameObject.GetComponent<Button>();
break;
}
}
// If none currently active, set to first tab
if (currentlyActive == null)
{
currentlyActive = tabs.GetChild(0).gameObject.GetComponent<Button>();
// Update colors
colorBlock = currentlyActive.colors;
colorBlock.normalColor = UISettings.activeColor;
currentlyActive.colors = colorBlock;
// Update height
currentlyActive.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(currentlyActive.transform.GetComponent<RectTransform>().sizeDelta.x, activeHeight);
}
// Ensure the correct 'body' is also active...
bodies.Find(currentlyActive.name).gameObject.SetActive(true);
// ...and all others are inactive
for (int loop = 0; loop < bodies.childCount; loop++)
{
if(bodies.GetChild(loop).name == currentlyActive.name)
continue;
bodies.GetChild(loop).gameObject.SetActive(false);
}
}
public void UpdateActive()
{
ColorBlock colorBlock;
Button buttonPressed;
// Get the button pressed
buttonPressed = EventSystem.current.currentSelectedGameObject.GetComponent<Button>();
// Guard clause if clicking currently active button
if (buttonPressed == currentlyActive)
return;
// Deactivate previously active tab + body
colorBlock = currentlyActive.colors;
colorBlock.normalColor = UISettings.inactiveColor;
currentlyActive.colors = colorBlock;
// Update height
currentlyActive.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(currentlyActive.transform.GetComponent<RectTransform>().sizeDelta.x, inactiveHeight);
bodies.Find(currentlyActive.name).gameObject.SetActive(false);
// Update active button var
currentlyActive = buttonPressed;
Debug.Log(currentlyActive.name);
// Update active button colors
colorBlock = currentlyActive.colors;
colorBlock.normalColor = UISettings.activeColor;
currentlyActive.colors = colorBlock;
// Update height
currentlyActive.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(currentlyActive.transform.GetComponent<RectTransform>().sizeDelta.x, activeHeight);
bodies.Find(currentlyActive.name).gameObject.SetActive(true);
}
}