• 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
Question by Afassolas · Oct 12, 2014 at 06:13 AM · guibuttonimage4.6

How to access SourceImage component of a button in new GUI 4.6

hello community,

in the new GUI a button has an Image script attached to it by default. Source Image is the field you put the image that the button will have. What i want is to be able to change that image in code c# at runtime after a condition is met.

Can you help me ?

thanks for your time

Comment
RetepTrun
Ylex
PersianKiller

People who like this

3 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by troien · Oct 13, 2014 at 09:45 AM

First of all, you need "using UnityEngine.UI" to be able to access Buton and/or Image components from your script. After that, all you need is a reference to the Image your button is using and then change the sprite or overrideSprite property's of that Image to your new Sprite

 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Button))]
 public class example : MonoBehaviour
 {
     public Sprite newsprite;
     public bool condition;
 
     private Button button;
 
     void Start ()
     {
         button = GetComponent<Button>();
     }
 
     void Update ()
     {
 
         if (condition)
         {
             // If you want to change the sprite for only a short time,
             // and use a default whenever your condition is false
             button.image.overrideSprite = newsprite;
 
             // But if you really want the source image,
             // use the following line instead
             // button.image.sprite = newsprite;
         }
         else
         {
             // Setting the overrideSprite back to null will cause
             // the image to display the original value of image.sprite again
             button.image.overrideSprite = null;
         }
     }
 }
Comment
RetepTrun
jchart7
Ziaxp
Dream_Architect
Ylex
Fredex8
javad_atrd
heo598
scruff311
martinasenovdev
PersianKiller

People who like this

11 Show 9 · 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 Afassolas · Oct 14, 2014 at 11:03 AM 0
Share

Thanks a lot this did it :) This can be applied to one button though, what if i have more buttons and i need to change the image on a specific one ?

avatar image troien · Oct 15, 2014 at 01:02 PM 0
Share

Im not certain if I understand this last question, because I believe this is possible by only adding this script to your specific button.

The only thing I can come up with is that you want to put this script on something else then a button, and assign your specific button to it through the inspector. To do this simply remove the "RequireComponent(typeof(Button))" attribute and the "Start" function. And make the "button" public to show it in the inspector...

avatar image Afassolas · Oct 15, 2014 at 01:24 PM 0
Share

I have several buttons and I need to change a specific one's image when a certain condition is met. If i apply this script to these buttons and this condition is met then all the buttons change with the new image not a specific one I want.

avatar image troien · Oct 15, 2014 at 01:42 PM 0
Share

Well, then you should add this script to your specific button that you want to change when your condition is met. DON'T add this script to any button that shouldn't change when the condition is met...

avatar image Afassolas · Oct 15, 2014 at 01:56 PM 0
Share
     void Cars(int carscore) {
             // changes the sprites to allow rewards of 1 , 2, 3 cars according to score
             images = gameObject.GetComponent<Image>(); // get the component of Image method

             if (carscore == 1) {
                 images.sprite = OneCarSprite;
     
     
             } else if(carscore == 2) {
                 images.sprite = TwoCarSprite;
     
     
             } else if(carscore == 3) {
                 images.sprite = ThreeCarSprite;
     
     
             } else images.sprite = NoCarSprite;
 
     
         }
  
Show more comments
avatar image

Answer by juharain · Nov 25, 2015 at 03:13 PM

You can do this without variables.

 GameObject.Find("carscore1").GetComponent<Button>().image.overrideSprite = null;

Now you can change the sprite in those buttons seperately. Just change the game objects name.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

On Click paramaters disappear from button prefab? 5 Answers

How to change button image on click 2 Answers

(4.6 UI) How to set up a window with x buttons, with a scroll bar? 1 Answer

GUI Button using .png image? 1 Answer

How can a button be highlighted but not trigger on click event when submit button is pressed? 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