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 12:55 PM · imagechange

Is it possible to change an images source image by code? C#

Im making a game were I want my background image to change between hundreds of different images that I have in my assets folder. Is there a way to do this by code? I have my Image "testImage" decleared in my script and I have dragged the Image gameobject to the slot in inspector. In my project assets I have the other source image that I want to change to called "NewSourceImage". I have tried with this code but it doesnt work:

   public Image testImage;
      
    public void Change()
      
      {
        testImage = GetComponent<Image>("NewSourceImage");
      }

I get the error message: error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments

Comment
Add comment · Show 2
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 Pradeep1040 · Nov 26, 2014 at 01:29 PM 0
Share

Yes.Its possible to change the any image at runtime.For that you need to use SpriteRenderer.I have this code written in c#.I have used this code and its works..enjoy..

//Change sprite on runtime

Sprite spr = Resources.Load ("Required Image");

SpriteRenderer sprRenderer = (SpriteRenderer)renderer;

sprRenderer.sprite = spr;

avatar image Hoffa25 · Nov 26, 2014 at 02:57 PM 0
Share

Thanks pradeep, but I cant get it to work. Sorry but Im new to unity and C#... I have changed my code to this now:

   public Image testImage;
    public Sprite spr;
 
       
     public void Change()
       
       {
                 spr = Resources.Load ("newSourceImage");
         sprRenderer = (SpriteRenderer)renderer;
         sprRenderer.sprite = spr;
         
       }

I now get error message: error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.Sprite'. An explicit conversion exists (are you missing a cast?)

Also with this code I never assign the sprite to the Image testImage. Im not sure how to do that.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Kiwasi · Nov 27, 2014 at 08:33 AM

Here is code to change an image

 public Image myImage;
 
 public void Change(){
     myImage.sprite = someOtherSprite;
 }

Here is code to load a sprite from the resources folder.

 public Sprite someOtherSprite;

 public LoadSprite (){
     someOtherSprite = Resources.Load<Sprite>("NewSourceImage");
 }

Here is both of them put together in a sensible structure.

 public void ChangeImage (string newImageTitle){
     myImage.sprite = Resources.Load<Sprite>(newImageTitle);
 }
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 Meowium99 · Oct 09, 2019 at 02:35 PM 0
Share

With this code being just under 5 years old, does it still work? I've tried to get it to work in my game, but not only am I having trouble actually calling the "ChangeImage" function, but I'm not exactly sure how to use this code to actually change a UI panel's Source Image. A little help please!

avatar image
0

Answer by hexagonius · Nov 26, 2014 at 04:16 PM

Resources.Load() is always returning an Object because it can nearly load anything, which means you need to cast the result into what the loaded resource will be.

 spr = Resources.Load ("newSourceImage") as Sprite;

or

 spr = (Sprite) Resources.Load ("newSourceImage");

My guess is that line 9 will also throw an error, because it does not match the preceding answer.

Comment
Add comment · Show 7 · 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 26, 2014 at 04:45 PM 0
Share

Thanx Hexagonius, but it still doesnt work for me :/. Now I get error message: "InvalidCastException: Cannot cast from source type to destination type." $$anonymous$$y sprite is a .jpg that I just have dragged from my coputer to my resources folder.

             Sprite spr = Resources.Load("newSourceImage") as Sprite;
         SpriteRenderer sprRenderer = (SpriteRenderer)renderer;
         sprRenderer.sprite = spr;
             testImage.sprite = spr;  // Here I try to put in the new sprite in the Image (am I correct?). 

avatar image hexagonius · Nov 26, 2014 at 04:51 PM 0
Share

Then I think your jpg is a Texture, not a sprite. Click on the jpg in the Project folder and look up the inspector window if it is and change it accordingly. What is testImage supposed to be? That line is not necessary because the 3rd one already does the assignment. 1. line: get the sprite 2. line: get the renderer 3. line: assign the sprite to the renderer

that's it

avatar image Hoffa25 · Nov 26, 2014 at 05:26 PM 0
Share

Honestly I dont understand why I need line two and three... testImage is my image that I need to change from code. I know how to change it manualy by draging a new image to the "Source Image" slot in my Image-script in inspector and thats what I want to do by code.

avatar image Hoffa25 · Nov 26, 2014 at 05:41 PM 0
Share

And you are correct, my jpg is a texture (texture2d)...

avatar image Hoffa25 · Nov 26, 2014 at 06:28 PM 0
Share

I dont know if it makes a diffrence but im using the unity 4.6 beta and my image is an UI image

Show more comments

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

7 People are following this question.

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

Related Questions

[Improvement] change image based on set value 1 Answer

How to change an images source image from script? C# 1 Answer

How to change an image after it turned 90 degree 1 Answer

Changing a sprite in image canvas VIA C# script 2 Answers

How I can change the sprite with resources.Load? 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