Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Notechis0 · Nov 10, 2020 at 09:02 PM · instantiateprefabspritefor-loop

How do I access an instantiated Image.sprite inside a GameObject inside a for-loop?

Hi everyone,

I hope someone can help me, I searched online for answers but I never found an answer, this is what I need: I have a FOR loop in which I instantiate a prefab I have prepared, inside that FOR loop I also change the .text value of the "Name" Text object, sort of like this:

 champPrefab.GetComponentInChildren<Text>().text = champName;

Where "champPrefab" is the prefab Object i created and "champName" is the value I want to give to the Text object. I would love to change the .sprite of an image inside that same loop of an Image object inside the prefab, but I don't know how to access it, or at least

 champPrefab.GetcomponentInChilden<Image>()

doesen't do the job.

For clarity I'll leave an image of the prefab and its hierarchy alt text I'm trying to access the highlighted object inside the for-loop I don't know if it helps but the other objects inside the Prefab are:

Prefab

Image

Image <= the one I want to access the .sprite of

Image

Text

Button

 public class StatsUpdate : MonoBehaviour
 {
    public GameObject PrefabChampIcon;
    public Transform ChampSelectParent;
    
    void Start()
    {
       foreach (var champ in champList) //"champ" is an item inside a List of local paths
       {
          GameObject champPrefab = Instantiate(PrefabChampIcon, transform.position, transform.rotation);
          champPrefab.transform.SetParent(ChampSelectParent);
          champPrefab.GetComponentInChildren<Text>().text = champName;
          StartCoroutine(GetImage(champPrefab.GetcomponentInChilden<Image>(), localPatVar));
       }
    }
   IEnumerator GetImage(Image _img, string _path)
     {
         UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(_path);
         yield return uwr.SendWebRequest();
         if (uwr.isHttpError || uwr.isNetworkError)
         {
             Debug.Log(uwr.error);
         }
         else
         {
             Texture2D imgTexture2d = ((DownloadHandlerTexture)uwr.downloadHandler).texture;
             _img.sprite = Sprite.Create(imgTexture2d, new Rect(0,0,120,120), Vector2.zero);
         }
     }
 }

Thanks in advance :P

prefabhierarchy.png (8.1 kB)
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 jackmw94 · Nov 10, 2020 at 11:08 PM

I think you're going down the wrong path with the web requests; these are used when you want to get some resource from a website or at some URL path!

The only thing blocking your from getting the Image the same way as you do the Text component is that there are multiple, meaning that if you only get one it's likely just going to give you the first one it finds. I think you have two options here: if this will be the last time you have to sort through multiple components then it's find to just use the GetComponentsInChildren function to return all the Images in the hierarchy, then search through them to find the one you want:

 private Image GetChampImage(GameObject champObject)
 {
     var allImages = champObject.GetComponentsInChildren<Image>();
 
     foreach (Image img in allImages)
     {
         if (img.gameObject.name.Equals("ChampImage"))
         {
             return img;
         }
     }
 
     Debug.LogError("Could not find champ image!");
     return null;
 }


Then inside your foreach-loop you can just call:

 Image champImg = GetChampImage(champ);
 champImg.sprite = ... // whatever you want to set the sprite to


However if you want to be a little more organised with this you can have a container for all these references that will sit on the root (champSelectFace) object. After you've added this, remember to drag the correct references in inside the prefab:

 public class ChampSelectFaceElements : MonoBehaviour
 {
     public Text ChampNameText;
     public Image ChampImage;
 }


So instead of then getting the text or searching for the image, you can instead just get the ChampSelectFaceElements and access the references via this! Inside your foreach-loop you can do this via:

 var champRefs = champFace.GetComponent<ChampSelectFaceElements>();
 champRefs.ChampText.text = champName;
 champRefs.ChampImage.sprite = // sprite!

Comment
Add comment · Show 5 · 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 Notechis0 · Nov 11, 2020 at 12:13 AM 0
Share

Thank You so so much for your answer, I took every option into consideration and deceided to go for the last one, I agree it is the best idea. This tought me a valuable lesson: planning goes a long way. In this case goes as far as to make the approach I had almost the worst idea possible. Will remember this for future scripting.

avatar image jackmw94 Notechis0 · Nov 11, 2020 at 01:48 AM 0
Share

Ah I wouldn't say worst idea, there's a 'path' to the gameobject so I see where you were co$$anonymous$$g from. Everyone's gotta start somewhere, hope you're enjoying it!

avatar image Notechis0 jackmw94 · Nov 11, 2020 at 12:54 PM 0
Share

On a side note I would be interested in knowing how you would go about getting images from a local file, searching the web i found many obsolete methods (www) and in the end I just went with "what worked". Should probably point out that I'm kid of new to unity (2 weeks into it) so if the questions are newbie-like that's because I'm still getting used to c# and how unity itself operates. (having a blast with unity / c# by the way ;) )

Show more comments

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

233 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 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 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

Change a prefab sprite using Random.range instantiate in a script 3 Answers

How to make a gameObject instantiate a prefab for every 250 damage taken ? 4 Answers

How to check if a component is on an instantiated prefab? 1 Answer

Lobbing a Bomb with Instantiate. 1 Answer

How can I instantiate multiple objects with a loop and have them be different 1 Answer


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