Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Disable/enable multiple images

Discussion in 'Scripting' started by AnAlienDream, Dec 29, 2019.

  1. AnAlienDream

    AnAlienDream

    Joined:
    Oct 19, 2019
    Posts:
    12
    So right now I have a piece of code to disable images that looks like this:
    Code (csharp):
    1. Cog1.enabled = true;
    2. Cog2.enabled = true;
    3. Cog3.enabled = true;
    4. Cog4.enabled = true;
    5. Cog5.enabled = true;
    And it works, but it's long and feels cumbersome. I've been trying to look up if there's a way to disable multiple images at once but I'm not sure how to do it. I tried to create a struct, but I couldn't use .enabled with it and I don't know if it's the right way to do it anyway. In this case all the images are children of a canvas, so I could also somehow disable all children.
    It would be nice if you can tell me the both ways to do this :)
     
  2. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    121
    you could make an array of images or a list instead of many single image references.
    then you can iterating over them in a for loop if your using an array or a foreach loop if you are using a list
     
    Last edited: Dec 30, 2019
  3. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    Maybe something like this:
    Code (CSharp):
    1.  
    2.  
    3. Image[ ] images = new Image[5];
    4.  
    5. void EnableImages()
    6. {
    7.      for (int i = 0; i < 5; i++)
    8.      {
    9.        images[i].enabled = true;
    10.      }
    11. }
    12.  
     
  4. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    An array and a foreach code would do:
    Code (CSharp):
    1.  
    2.     public GameObject[] cogImages;
    3.  
    4.     public void EnableImages()
    5.     {
    6.         foreach(GameObject item in cogImages)
    7.         {
    8.             item.gameObject.SetActive(true);
    9.         }
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         EnableImages();
    15.     }
    16.  
     
    Last edited: Dec 30, 2019
  5. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746

    foreach used to be frowned upon, as it is (was?) less performant than a for loop.

    item is already a GameObject, so you can just do item.SetActive(true);

    Don't call it from Update() ..
     
  6. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Ah yes, I took the example from another type of objects where components were to be accessed.

    So, how would you use for in this case?
    Code (CSharp):
    1.     public GameObject[] cogImages;
    2.  
    3.     public void EnableImages()
    4.     {
    5.         for(int i = 0; i < cogImages.Length; i++)
    6.         {
    7.             gameObject.SetActive(true);
    8.         }
    9.     }
    10.  
     
    Last edited: Dec 30, 2019
  7. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    @APSchmidt

    Code (CSharp):
    1. for(int i = 0; i < images.Length; i++)
    2. {
    3.      images[i].enabled = true;
    4. }
     
  8. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Ah, thank you for refreshing my memory. :)

    As for using ii in Update(), you can do that, as long as you take some precautions. What about this?
    Code (CSharp):
    1.     public GameObject[] cogImages;
    2.     public bool imagesEnabled = false;
    3.  
    4.     public void EnableImages()
    5.     {
    6.         if(!imagesEnabled)
    7.         {
    8.             for(int i = 0; i < cogImages.Length; i++)
    9.             {
    10.                 cogImages[i].enabled = true;
    11.             }
    12.         imagesEnabled = true;
    13.         }
    14.     }
     
  9. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Way back when it used to create some GC but that's no longer the case. I think raw for loops are faster but with something as simple as this I'd say using for > foreach is micro optimizing and making the code a little more ugly.
     
  10. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    I like you. :)
     
  11. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Thanks for the clarification. The ugliness is subjective though, due to using for loops for so long they look better IMO than a foreach.

    I mean, you could do that, but you shouldn't be using Update to call something that doesn't happen every frame. Your logic is also wrong here, you could still end up in the loop multiple times, you'd need to set imagesEnabled to true before the loop starts.
     
  12. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    I didn't test the script before posting it, neither after for that matter. I'll have to do it once to see what happens. :)