- Home /
How do I make an array of buttons open corresponding images on click?
I have an array of images, and an array of buttons. I want the buttons to activate each respective image when clicked. Both arrays are the same size, and they should correspond in index (button 0 will enable image 0, button 1 will enable image 1, etc). I'm trying to make the script generic because in some of my scenes, I will have more images than others, so I didn't want to make a separate script for different scenes.
I am stuck on the click part... I can't figure out how to tell match the button index to the corresponding image index. When I return the IndexOf, it tells me -1.... not sure how to get a specific index value of each object in the array, and then pass that on to the button with the same index. Forgive me because I am still pretty new to C#. Any help is greatly appreciated.
public GameObject[] images;
public GameObject[] buttons;
public void Start()
{
foreach(GameObject i in images)
{
i.SetActive(false);
}
}
void Update()
{
}
public void ButtonClick()
{
{
int index1 = System.Array.IndexOf(buttons, gameObject);
int index2 = System.Array.IndexOf(images, gameObject);
Debug.Log(index1);
Debug.Log(index2);
}
}
}
Answer by Hellium · Jan 23, 2019 at 02:57 PM
// Drag & drop the gameObjects to enable in the inspector
public GameObject[] Images;
// Drag & drop the buttons in the inspector
public Button[] Buttons;
public void Start()
{
if( Images.Length != Buttons.Length )
Debug.LogWarning( "You don't have the same number of images and buttons !");
AddButtonsListeners();
// Uncomment if needed
//DisableImages();
}
///<summary>
/// Add the listeners to each button
///</summary>
private void AddButtonsListeners()
{
int length = Images.Length > Buttons.Length ? Buttons.Length : Images.Length;
for( int index = 0 ; index < length ; ++index )
{
if( Buttons[index] != null )
AddButtonListener( Buttons[index], index );
}
}
///<summary>
/// Add a listener to a single button
///</summary>
private void AddButtonListener( Button button, int index )
{
button.onClick.AddListener( () =>
{
// Enable the following comment if you want only one image enabled at a time
//DisableImages();
EnableImage( index );
// If you want to toggle the image instead of just enabling it,
// uncomment the following line and comment the previous one
//ToggleImage( index ) ;
} );
}
///<summary>
/// Disable the images
///</summary>
private void DisableImages()
{
for( int imageIndex = 0 ; imageIndex < Images.Length ; ++imageIndex )
{
if( Images[imageIndex] != null )
Images[imageIndex].SetActive( false ) ;
}
}
///<summary>
/// Enables one image
///</summary>
private void EnableImage( int imageIndex )
{
if( imageIndex >= 0 && imageIndex < Images.Length && Images[imageIndex] != null )
Images[imageIndex].SetActive( true ) ;
}
///<summary>
/// Toggles one image
/// If the image was disabled, enable it
/// If the image was enabled, disable it
///</summary>
private void ToggleImage( int imageIndex )
{
if( imageIndex >= 0 && imageIndex < Images.Length && Images[imageIndex] != null )
Images[imageIndex].SetActive( !Images[imageIndex].activeSelf ) ;
}
Thanks, @Hellium! At first I thought it wasn't working but then realized in "EnableImages," setActive was false ins$$anonymous$$d of true. Once I changed that, bingo. Works like a charm. I wish I understood the language a little better, but nonetheless I greatly appreciate the help.
$$anonymous$$y bad, I fixed the answer ;)
I tried to make it as clear and organized as possible, with small functions and comments.
Yeah, I really appreciate the comments. They are definitely helpful. Just some things I haven't encountered yet into my brief C# history :) That is how I learn though.