Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jdpeters79 · Jan 23, 2019 at 02:24 PM · arraysclickindexof

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

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 ) ;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jdpeters79 · Jan 23, 2019 at 03:17 PM 0
Share

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.

avatar image Hellium jdpeters79 · Jan 23, 2019 at 03:22 PM 1
Share

$$anonymous$$y bad, I fixed the answer ;)

I tried to make it as clear and organized as possible, with small functions and comments.

avatar image jdpeters79 Hellium · Jan 23, 2019 at 03:27 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find x item in list? 2 Answers

Comparing an object with a Serializible Array with same object returns -1? 0 Answers

Basic object creation in arrays! Syntax error! 1 Answer

Assets/Scripts/Enemy.cs(61,44): error CS1955: The member `UnityEngine.GameObject.tag' cannot be used as method or delegate 1 Answer

Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges