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
1
Question by BrandonD · Oct 13, 2014 at 02:54 PM · 4.6

Change Unity 4.6 UI Image element's sprite via script?

Hi there! I'm using the following code to generate a series of buttons with images on top of them, which for the most part is working fine:

 function RedefineScrollbar()
 {
     for(var i=0;i<curComponents.length;i++)
     {
         var newButton = Instantiate(componentButton,Vector3(0,0,0),Quaternion.identity);
         newButton.parent=shipComponentHolder;
         var thumb : RectTransform = newButton.Find("Thumbnail");
         thumb.GetComponent(UnityEngine.UI.Image).sprite = curComponents[i].thumbnail;
     }
 }

This compiles fine, but the last line causes the following error on every frame:

MissingReferenceException: The object of type 'Sprite' has been destroyed but you are still trying to access it.

Here's what the image I'm accessing looks like as a prefab:

http://i.imgur.com/CLbrQJv.jpg

And here is the jumbled mess it looks like as it actually gets instantiated:

http://i.imgur.com/hnkCpjM.jpg

Any insight as to why this is happening would be greatly appreciated!

Comment
Add comment · Show 1
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 aman_jha · Oct 13, 2014 at 03:17 PM 0
Share

I'm not really sure but it looks like a bug. Remember Unity 4.6 is a beta release. You should probably tell them if it doesn't get fixed.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by _dns_ · Oct 13, 2014 at 03:19 PM

Hi, I'm modifying Image.overrideSprite and it works well.

Comment
Add comment · Show 4 · 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 BrandonD · Oct 13, 2014 at 03:35 PM 0
Share

This has restored the prefab to working order in the inspector, and all looks well, but the error persists and it is still not rendering correctly. Thank you, this is a step in the right direction.

avatar image _dns_ · Oct 13, 2014 at 03:45 PM 0
Share

So this must be that the "curComponents[i].thumbnail" sprites are already destroyed. Where do they come from ?

avatar image BrandonD · Oct 13, 2014 at 03:49 PM 0
Share

They come from a class within the same script, and are defined in the inspector, as in this screenshot.

http://i.imgur.com/lwlAi4C.jpg

They still look normal at runtime, as well.

avatar image BrandonD · Oct 13, 2014 at 03:56 PM 0
Share

Got it! I had changed the import settings of the sprite to sprite, but hadn't reassigned this to the class component, meaning they were still trying to accept it as a texture2D! So I had two problems I suppose! Thanks for your help!

avatar image
1

Answer by Raynoko · Feb 02, 2015 at 01:32 PM

here is solution

 public Sprite mySprites;
 
     public void Start()
     {
         GetComponent<UnityEngine.UI.Image>().sprite = mySprites;
     }




Comment
Add comment · Show 1 · 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 Kohouri · Sep 05, 2015 at 02:18 AM 0
Share

Verified this works in newest version 5.0.1f

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can I make multiplayer game in Unity 4.7? 1 Answer

New UI system. How to focus on input fields. 6 Answers

4.6UI Rect Transfrom fix 1 Answer

Unity 4.6 beta button with Javascript rather than C# 2 Answers

Is it possible to start a coroutine using a button from the new UI system (4.6) 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