简体   繁体   中英

Check if Button was not selected unity

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);
    }
}

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
        }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM