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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to find a image of a certain name

Discussion in 'Scripting' started by Sean-Powell, Jul 6, 2015.

  1. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    Hi i am looking for a image in my scene so i can set that to a Image reference in my script, how would i do it i have tried this but it says can not convert Unity.Engine.Ui.Image to System.Collections.Generic.List<Unity.Engine.Ui.Image>

    here is the code
    Code (CSharp):
    1. List<Image> images = new List<Image> ();
    2.  
    3. images = (Image) FindObjectOfType (typeof (Image));
    4.         for (int i = 0; i < images.Count; i++) {
    5.             if(images[i].name == "Resource_Counter"){
    6.                 resourceCounter = images[i];
    7.                 Debug.Log(resourceCounter);
    8.                 images.Clear();
    9.                 break;
    10.             }
    11.         }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You want FindObjectsOfType and you want an array, not a list.

    Code (csharp):
    1.  
    2. Image[] images = FindObjectsOfType(typeof(Image)) as Image[];
    3.  
     
  3. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    thanks, though a list would work the same but thanks
     
  4. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    that code throws
    Assets/Scripts/Gui Scripts/SpawnMiner.cs(15,77): error CS0039: Cannot convert type `UnityEngine.UI.Image' to `UnityEngine.UI.Image[]' via a built-in conversion
    i tried typecasting it too
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Did you add the S to FindObjectsByType()? Can you post the entire script?
     
  6. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    i did it a different method so i no longer need it but thanks for the help