- Home /
Runtime Changes to UI Object From Prefab Unexpectedly Changing All Prefab Objects
(having some formatting issues, lines that are not indented are still showing up in code block. Sorry)
I have a prefab that has the following hierarcy as follows when instantiated:
The top level item (in the above case Feed Item 0), has a script attached to it called FeedItemScript. FeedItemScript uses the following code in Awake() to gain access to it's subObjects and components.
//all variables below were declared null at class level previously
//connect to everything we want to customize
userImageObject = transform.Find("User Mask/User Image").gameObject;
userImage = userImageObject.GetComponent<Image>();
nameObject = transform.Find ("Name Text").gameObject;
nameText = nameObject.GetComponent<Text>();
fizzUpCountObject = transform.Find ("Fizz Up Value").gameObject;
fizzUpCountText = fizzUpCountObject.GetComponent<Text>();
timeObject = transform.Find("Time Text").gameObject;
timeText = timeObject.GetComponent<Text>();
fizzImageObject = transform.Find("Fizz Image").gameObject;
fizzImage = fizzImageObject.GetComponent<Image>();
titleObject = transform.Find("Title Text").gameObject;
titleText = titleObject.GetComponent<Text>();
pointsObject = transform.Find ("Points Text").gameObject;
pointsText = pointsObject.GetComponent<Text>();
descriptionObject = transform.Find ("Description Text").gameObject;
descriptionText = descriptionObject.GetComponent<Text>();
I have a function called SetProfilePic that looks as follows:
IEnumerator SetProfilePic () {
WWW www = new WWW (picUrl);
//let other stuff happen while we wait for the download
yield return www;
//test if we got back an image or a 404.
if (!www.text.Contains("html")) {
www.LoadImageIntoTexture (userImage.sprite.texture);
}
www.Dispose ();
www = null;
}
What's surprising is that this script will change the image for every object, not just the object that the script is attached to. If I create 64 feed items, I will watch the image for the first item change 63 more times, once for each of all the new objects.
I don't think prefabs are supposed to be linked in this way. Is there something I can do to break the link?
Answer by Noah Dyer · Feb 06, 2015 at 06:02 PM
I figured it out. I should have been creating a new sprite with the texture, not changing the texture on the existing sprite. The latter changes the underlying texture for every image that is using the sprite, not the image for just the individual sprite instance you are working with. Final code looks something like:
Texture2D profileTex = new Texture2D(www.texture.width, www.texture.height);
www.LoadImageIntoTexture (profileTex);
Sprite profileSprite = Sprite.Create(profileTex, new Rect(0, 0, profileTex.width, profileTex.height), new Vector2(0,1));
userImage.sprite = profileSprite;
Your answer
Follow this Question
Related Questions
Injecting data into Prefabs 1 Answer
SetActive true not working on UI object 2 Answers
UIPrefab from prefab Canvas to live Canvas 1 Answer
Canvas with GUI elements in prefab act strangely 0 Answers
Display text above prefabs in Unity 4.6 0 Answers