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 Hoffa25 · Nov 26, 2014 at 10:14 PM · imageloadchange

(Unity 4.6 beta) Change UI Image source image from code? C#

I need to change the source image (sprite) in my image script attached to my gameobject. Its easy to do manually by just draging an image from my resourses folder to the "source image slot" in inspector, but how do i do it in code? This is my atempt but it oly results in the image changing to just being white (the color i have chosed in inspector).

 public Image testImage; // The one that is default when game starts
 public void ChangeImage() {
 
     testImage.sprite = Resources.Load ("newImage") as Sprite;
 
 }

What is really odd is that it doesnt matter if I type "newImage" correctly..? Unity doesnt complain when it loads a non existing imgage in my resourses folder.

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 AndrewBilotti · Jan 13, 2016 at 08:58 PM 0
Share

Great question!

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by deadlyfingers · Jul 30, 2015 at 10:39 AM

If you have an Image sprite already set in Unity3D and you want to change it later you can use overrideSprite:

 public void ChangeImage() {
     // Sprite.Create params: Sprite.Create(Texture2D, Rect, Vector2d)
     testImage.overrideSprite = Sprite.Create( Resources.Load<Texture2D>("newImage"), testImage.sprite.rect, testImage.sprite.pivot );
 }
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
avatar image
2

Answer by Kiwasi · Nov 26, 2014 at 11:04 PM

Unity returns null if the resource can't be found. Casting using as will return null if the type is incorrect. An Image component with a null sprite renders white.

I would suggest using the generic version of Resources.Load. I would also suggest logging an error is this is null

 Sprite newSprite =  Resources.Load <Sprite>("newImage");
 if (newSprite){
     testImage.sprite = newSprite;
 } else {
     Debug.LogError("Sprite not found", this);
 }
Comment
Add comment · Show 3 · 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 Hoffa25 · Nov 27, 2014 at 06:31 AM 0
Share

Thanks Bored$$anonymous$$ormon! Now it says sprite not found when I type incorrectly! However when typed correctly I get this error message: "InvalidCastException: Cannot cast from source type to destination type. UnityEngine.Resources.Load[Sprite] (System.String path) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/BaseClass.cs:708)"

Is there something wrong with my pictures I have in my resources folder? They are both .jpg and .psd and noone works. What confuses me is that they dont seem to be sprites... but ins$$anonymous$$d they are textures? So dont I have to attach the texture to a sprite first before I can change sprite on my testImage?

avatar image Kiwasi · Nov 27, 2014 at 08:41 AM 0
Share

$$anonymous$$y code shouldn't throw a casting error!

You can change from textures to sprites in the import settings. Just click on the picture and change the texture type to sprite.

Sprite is the 2D version of a texture.

avatar image cwperkins83 · Dec 18, 2014 at 09:25 PM 0
Share

Oh my god. Been searching for a solution to this problem for days. Thanks, bored.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to load an image from file path? 1 Answer

Load level after Image Sequence 0 Answers

carrying player data from one scene to the next 1 Answer

load images in sequence into a gui? 2 Answers

Asyncchronous image loading 2 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