0

Hi My scene name is a game.In that scene I am having A main panel that's name is ITEMCONTAINER. In item container I am having a panel whoes name is ITEM. I attached a script in ITEM PANEL. IN that script i am having a game object public,a raw image ,text and how many times loop will continue are public. In the place of game object i attached my prefab,that contain 1 text and 2 rawimage. in place of text i attached text component of prefab and same like raw image. When I run the game the text value I am getting correctly but rawimage is showing blank in runtime.Here i am running my loop 3 times and all three times it create clone of my prefab panel as a child in itempanel I want rawimage dynamic at my run time

output

enter image description here

prefab

enter image description here

  1. image= in this image , it contains output.here rawimage is blank but text valuecoming perfectly

  2. image = it is my prefab that prefab will be clone during runtime, here it shows image but during runtime , in clone it shows blank

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;   
    using UnityEngine.UI;    
    public class DynamicData : MonoBehaviour {      
    public GameObject prefab; 
    public Text name;       
    public int numberToCreate;
    
    public RawImage profile;
    
    
    void Start () {
    
        for (int i = 0; i < numberToCreate; i++)
        {
            name.text = "a"+i;
            StartCoroutine( ImageDownload( profile));
    
            Instantiate <GameObject>(prefab, transform);
        }
    
    }
    IEnumerator ImageDownload ( RawImage img) {
    
        WWW www = new WWW("https://www.w3schools.com/w3images/fjords.jpg");
    
        yield return www;
    
        Texture2D texure = new Texture2D (1, 1);
        texure.LoadImage (www.bytes);
        texure.Apply ();
        img.texture = texure;
    
    }
    

    }

Mr R
  • 13
  • 6
  • It's unity not unity container and the language is c# – Programmer Oct 01 '17 at 09:34
  • yes. I saw in previous post u help one people.please help me ,if u want i can send my unity package to u – Mr R Oct 01 '17 at 09:35
  • What is the prefab and where is this script attached? – Programmer Oct 01 '17 at 09:41
  • wait 2 min i will send the link of my unity package – Mr R Oct 01 '17 at 09:42
  • here prefab will come on runtime. and script is attached to item panel.that item panel is inside of item container – Mr R Oct 01 '17 at 09:43
  • I will not download package. We help people not download their project. That's what I do as a last resort. You want to instantiate a prefab and set the image to the downloaded image? I just don't understand what you are even doing so please edit your question and explain. – Programmer Oct 01 '17 at 09:45
  • i just want rawimage will be download and it will show during runtime.If my loop is running 5 times,then 5 image. – Mr R Oct 01 '17 at 09:54
  • here is thelink https://drive.google.com/file/d/0B-WC5oM3eq1tX2c0dWlUSGRQNGc/view?usp=sharing – Mr R Oct 01 '17 at 10:02

1 Answers1

0

You are assigning the downloaded image to the profile variable which is not a variable from the instantiated Object. In the loop, you need to get the child of each instantiated Object with transform.Find("profile") then get the RawImage component attached to it with GetComponent and pass it to the ImageDownload function.

Replace your for loop with this:

for (int i = 0; i < numberToCreate; i++)
{
    name.text = "a" + i;

    GameObject instance = Instantiate<GameObject>(prefab, transform);
    //Get RawImage of the current instance
    RawImage rImage = instance.transform.Find("profile").GetComponent<RawImage>();
    StartCoroutine(ImageDownload(rImage));
}

Please rename public Text name; to something else like public Text userName; because the name is already declared in MonoBehavior you derived your script from.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks alot. You helped me.I was struggling from past 1 week. 1 more question, suppose my rawimage have some tag then how i will call and do operation like – Mr R Oct 01 '17 at 10:08
  • `GameObject.FindGameObjectWithTag("yourtag").GetComponent();`....You are welcome! – Programmer Oct 01 '17 at 10:10
  • Thanks for your help.Here my tag is Profile for rawimage, Then how i will apply in in same program.Can you show me code – Mr R Oct 01 '17 at 10:12
  • Please create new question about tag since this is totally a different issue. I will see if I can help. – Programmer Oct 01 '17 at 10:14
  • here you are doing : RawImage rImage = instance.transform.Find("profile").GetComponent(); so here you are finding by his name,so i want like find by tag – Mr R Oct 01 '17 at 10:15