Quantcast
Channel: Questions in topic: "httpwebrequest"
Viewing all articles
Browse latest Browse all 190

How to insert webRequest data to prefabs before instantiation

$
0
0
I have a script in which I want to inject data to several prefabs before I instantiate them. my idea was to make a "for" loop in which every time I send a url from a url list (string) and inject the response into the sprite list (later on there will also be additional data) and then instantiate. The problem is- unity doesn't wait for a response, so I get empty prefabs. I tried this: [SerializeField] GameObject prefab, container; public int amount = 10; private GameObject PowerBG, StaminaBG; public Sprite[] images; public string[] imagesURL; void Start() { foreach (Transform child in transform) { GameObject.Destroy(child.gameObject); } for (int i = 0; i < amount; i++) { WebRequest.GetTexture(imagesURL[i], (string error) => { //error Debug.Log("Error: " + error); }, (Texture2D texture2D) => {//successfuly contacted url Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0, 0)); prefab.transform.Find("Avatar").GetComponent().sprite = sprite;//get image from Server prefab.transform.Find("Name").GetComponent().text = "test " + (i).ToString();//get name from server PowerBG = prefab.transform.Find("PowerBG").gameObject; StaminaBG = prefab.transform.Find("StaminaBG").gameObject; PowerBG.transform.Find("FillPower").GetComponent().fillAmount = .5f; StaminaBG.transform.Find("FillStamina").GetComponent().fillAmount = .5f; Instantiate(prefab, container.transform.position, Quaternion.identity, container.transform); } ); } } } but all prefabs are appearing with the same name. I would really appreciate some help :) Thanks!

Viewing all articles
Browse latest Browse all 190

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>