Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Noah Dyer · Feb 04, 2015 at 07:26 PM · uiprefabprefabs

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:

prefab hierarcy

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?

screen-shot-2015-02-04-at-115743-am.png (30.3 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
0

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;
Comment
Add comment · 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

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

19 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

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


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