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
6
Question by LT23Live · Apr 28, 2015 at 11:00 PM · uiguispritetexture2dimage

Unity 5.0 Trying to Change the Image - Source Image via Script

So the title says a big portion of what I am trying to do. I am trying to use an external script to change the Source image of the UI > Image Although I dont quite know what variable to use.

I am making a GUI feed back system for a battle queue. Sort of like Biowares game Dragon Age or Star Wars Knights of the old republic. So I can queue the attacks. I already got the attack queue working so I just need this as visual feedback.

Your help would be greatly appreciated.

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 LT23Live · Apr 28, 2015 at 11:14 PM 0
Share

Heres my script code so far.

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class queueRead : $$anonymous$$onoBehaviour {

 public GameObject queue;
 public Sprite attack1;
 public Sprite attack2;
 public Sprite attack3;
 public Sprite attack4;
 public Image attack1Display;
 public Image attack2Display;
 public Image attack3Display;
 public Image attack4Display;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 if (queue.GetComponent <battleQueue>().queue[0] == 0)
     {
     
     }
 }

}

avatar image Addyarb · Apr 28, 2015 at 11:34 PM 0
Share

So far so good, though if you're queuing things, you will probably end up using a for/for-each loop.

Can you explain a bit more on what you're trying to achieve? I'm not familiar with the references you mentioned unfortunately.

3 Replies

· Add your reply
  • Sort: 
avatar image
20

Answer by Addyarb · Apr 28, 2015 at 11:12 PM

Hi there! Sorry to hear you're having trouble swapping out images.

The Unity 4.6UI+ system is a bit different for sure. For one thing, the "Image" component uses sprites, which is a little confusing. However, here is how to properly switch them out.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class ImageTestClass : MonoBehaviour {
     // Drag your image component here in the inspector window
     public Image myImageComponent;
 
     // Drag your first sprite here in the inspector window
     public Sprite myFirstSprite;
 
     // Drag your second sprite here in the inspector window
     public Sprite mySecondSprite;
 
     // Call this method somewhere to set your image's sprite to myFirstSprite
     public void SetImage1() {
         myImageComponent.sprite = myFirstSprite;
     }
 
     // Call this method somewhere to set your image's sprite to mySecondSprite
     public void SetImage2() {
         myImageComponent.sprite = mySecondSprite;
     }
 }

Hope this has helped you. Good luck, and feel free to ask if you have any questions!

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 LT23Live · Apr 29, 2015 at 12:32 AM 0
Share

thank you!

avatar image pane21 · Apr 25, 2016 at 06:18 AM 4
Share

perfect just don't forget to add the using UnityEngine.UI;

avatar image andrewwebber25 pane21 · Jul 11, 2017 at 08:36 PM 0
Share

Good Tip, didnt work without that!

avatar image meethi pane21 · Jul 30, 2020 at 07:04 AM 0
Share

Thank you!

avatar image RandomCharacters · Jun 24, 2016 at 04:28 AM 0
Share

worked for me but I referenced a button on another game object.

 using UnityEngine.UI;
 
 //Used to change the image of music
         public GameObject musicToggleButton;    
 //must be public as game object start sout off
     Image myImageComponent;
     public Sprite myFirstImage;
     public Sprite mySecondImage;
 
 void Awake()
 //Set the button for music so we can change the icon
     myImageComponent = musicToggleButton.GetComponent<Image>();
 
         //////////////////////////////////////////////////////////////
         /// Toggle the sound. Cycle through all sound modes and set the volume and icon accordingly
         public void Toggle$$anonymous$$usic ()
         {
             if (currentState$$anonymous$$usic == volumeOn) 
             {
                 currentState$$anonymous$$usic = volumeOff;
                 myImageComponent.sprite = mySecondImage;
                 } 
             else 
             {
                 currentState$$anonymous$$usic = volumeOn;
                 myImageComponent.sprite = myFirstImage;
             }
 
             SetSound();
         }
avatar image junedmmn · Jul 31, 2017 at 08:06 AM 0
Share

Error: Object reference not set to instance of an object myImageComponent.sprite = myFirstImage;

avatar image junedmmn junedmmn · Jul 31, 2017 at 08:19 AM 0
Share

$$anonymous$$y Error was solved. I removed statement -- myImageComponent = GetComponent(); from Start() and it worked

avatar image
0

Answer by epicpython · Oct 15, 2017 at 07:23 PM

In Unity 5.6, you have to use this.GetComponent<SpriteRenderer>().sprite= myfirstImage; instead of

  myImageComponent = GetComponent<Image>(); //Our image component is the one attached to this gameObject.
 
  myImageComponent.sprite = myFirstImage;

 
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 toloco3 · Oct 21, 2019 at 07:23 PM 0
Share

YES! Thanks! That worked! (On 2019).

avatar image
0

Answer by ilozen · Jun 13, 2018 at 10:02 AM

I have the same case but I don't have a SpriteRenderer so the solution provided by @epicpython does not work for me. I just have an Image directly placed, so it has a CanvasRenderer.

Any idea on how to get this to work?

Edit: Found the solution! I had to change both Image.sprite and Image.overrideSprite and then it started updating properly.

Comment
Add comment · Show 2 · 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 epicpython · Jun 13, 2018 at 04:24 PM 0
Share

I had a similar situation, this is what I did:

  using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class FileFolderWindow : $$anonymous$$onoBehaviour {
     
         public Image report; //change this image
         public Sprite[] spri$$anonymous$$rray;//an array of images that will be cycled through
         int index = 0;//start at index = 0
     
         void TestNextFunction()
         {
             index += 1;
             if (index > spri$$anonymous$$rray.Length - 1) {//if index is too big
                 index = 0; //loop back around
             }
             report.sprite = spri$$anonymous$$rray[index];
         }
         void TestBackFunction()
         {
             index -= 1;
             if(index < 0)//if index is too small
             { index = spri$$anonymous$$rray.Length - 1;}//loop back around
             report.sprite = spri$$anonymous$$rray[index];
     
         }
     }

I set the array of images and the public Image report in the inspector.

avatar image epicpython · Jun 13, 2018 at 04:28 PM 0
Share

Simpler version (no array):

  public class FileFolderWindow : $$anonymous$$onoBehaviour {
          public Image report; //change this image
          public Sprite sprite1;//set in the inspector
      
          void ChangeSprite()
          {
              report.sprite = sprite1;
          }
     }

Also note that because the image is set in the inspector, you can attach this to a different object than the image (that's how I did it).

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

31 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 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

How to create Sprite programmatically 1 Answer

Transparent Images as UI 0 Answers

Check if UI element is fully inside another UI element 1 Answer

Interface relative scaling 0 Answers

Why is texture blurred in one GuiStyle and not in another? 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