Home > Software design >  Check if Button was not selected unity
Check if Button was not selected unity

Time:09-23

I am a bit confused. I have this script on each button and GameObject newButton is assigned to its own button. I am trying to tell the other buttons if it is not the one who was selected change your color. However, I haven't been able to get it to work on detecting if it wasn't selected.

using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
public class TheLobbySelectorButtons : MonoBehaviour, ISelectHandler
{ 

    public GameObject newButton;
    Button[] buttonsArray;

    public void Awake()
    {
        buttonsArray = FindObjectsOfType<Button>();
    }
    public void OnSelect(BaseEventData eventData)
    {
        if (eventData.selectedObject == newButton)
        {
            Debug.Log(this.newButton.name   " was selected");
        }
        if (eventData.selectedObject != newButton)
//this is where I dont know how to fix 
//and dont know whats wrong.
        {
            newButton.GetComponent<Image>().color = new Color32(33, 49, 183, 156);

        }
    }

public void Click()
    {
        Debug.Log("Bruh");
        newButton.GetComponent<Image>().color = new Color32(165, 173, 248, 190);
    }
}

CodePudding user response:

Debug.Log("Bruh"); hahaha! sometimes I use debug.log purely just to express frustration too lol.

Try this:

    public void OnSelect(BaseEventData eventData)
    {
        // TO DO: Change the colors of all buttons
    
        if (eventData.selectedObject == newButton)
        {
        // TO DO: Revert the color of newButton to its default color
        }
    }

CodePudding user response:

If I am understanding correctly, you want to change the color of all buttons which are NOT selected, right? Maybe keep track of all buttons that you have (add them to a list at instantiation or through the start method) and then go through that list and compare each button to the selected button. If it is not the selected button/game object then change the color, otherwise leave it as it is.

  • Related