- Home /
Change properties of a button by pressing another button in Unity
Good day, I have a problem in Unity that I could not solve, the only thing I want is that by pressing a button to change the image of other 3 buttons, the idea is that I have 30 buttons and if I press any of them, I change the Image property of another 3 buttons depending on the button that you press before. This is the script that I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Experimental.UIElements;
public class Controller : MonoBehaviour {
public Camera cam1, cam2;
public Sprite[] mayus;
public AudioClip[] clipLetras;
public AudioSource fuente;
public GameObject imagenMayus, imagenMinus, imagenManus;
new string name;
public Image imgMayus, imgMinus, imgManus;
// Use this for initialization
void Start () {
cam2.gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
}
public void MoverCamara()
{
name = EventSystem.current.currentSelectedGameObject.name;
cam1.gameObject.SetActive(false);
cam2.gameObject.SetActive(true);
//cam1.gameObject.transform.position = cam2.transform.position;
//cam2.gameObject.SetActive(true);
}
public void RegresarMenuLetras()
{
cam1.gameObject.SetActive(true);
cam2.gameObject.SetActive(false);
}
public void ReproducirClip()
{
for (int x = 0; x < mayus.Length; x++)
{
if (mayus[x].name == name)
{
fuente.clip = clipLetras[x];
fuente.Play();
Debug.Log("entra " + mayus[x].name);
Debug.Log("entra " + name);
Debug.Log("entra " + x);
}
}
}
public void CambiarBotones()
{
}
}
In the change buttons method, I would like to change the Image property of 3 different buttons.
Answer by hectorux · Nov 06, 2018 at 06:01 PM
You could make a class called something like BotonesACambiar and store there the buttons you want to change if you press that button, atach to that button the script (one per button), and finally make a method to change your 3 buttons to the images you wanted. I recomend this because if every button will have therir custom 3 buttons, then is better to assign them manually. A button can have more than one listener to play