Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
2
Question by karlkevin · Apr 16, 2016 at 09:58 PM · gameobjectbuttoncanvasimagegetcomponent

How to change image of button when clicked?

@karlkevin

Hello, I am making a on-off button, so the idea is that at first i have a button with image "ON", so i click it, it will change stuff- which i have already coded, but it has to change the button image to "OFF" (I have already made the pictures).

I tried something with gameObject.getComponent (); but it doesn't help me to get to the image what is attached to button in "source image" I hope somebody knows how to pass this little problem. Thank you for your help! :)

Comment
Add comment · Show 4
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 Namey5 · Apr 17, 2016 at 10:23 AM 0
Share

This hasn't really explained much as to what your problem is, what GUI systems you are using, etc. Please expand.

avatar image karlkevin Namey5 · Apr 17, 2016 at 01:23 PM 0
Share

So im trying to change the picture of 1st screenshot where it sats "Source image"- there is a sprite atm "soundson", so this is my first picture, and when I press that button, it should change it to sprite, which I have made- "soundsoff". Hope this helps to undrestand what I'm trying to do :). alt text

alt text

screen-shot-2016-04-17-at-41912-pm.png (42.0 kB)
screen-shot-2016-04-17-at-41937-pm.png (9.7 kB)
avatar image Namey5 karlkevin · Apr 17, 2016 at 01:46 PM 0
Share

Sorry, my bad. I thought you were using GUI scripting. Yeah, Eugenius wrote the solution. You just have to find the variable name. All this stuff is documented in the API:

http://docs.unity3d.com/ScriptReference/index.html

Show more comments

2 Replies

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

Answer by Eugenius · Apr 17, 2016 at 12:35 PM

You probably need to specify at the beginning of your script:

using UnityEngine.UI (C#)

And for reaching the image of your button, you need to do:

 gameObject.GetComponent<Image>().image

Hope this helps!

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
11

Answer by Ali-hatem · Apr 17, 2016 at 01:24 PM

 public Sprite OffSprite;
 public Sprite OnSprite;
 public Button but;
 public void ChangeImage(){
     if (but.image.sprite == OnSprite)
         but.image.sprite = OffSprite;
     else {
         but.image.sprite = OnSprite;
     }
 }

the above example will change button image each time you click . but if you want to change once only from On to Off you don't need any coding ! . note the script is attached to empty game object so drag & drop to On Click event you know the rest but if you don't know how to use On Click event see this tutorial Make buttons do something.

Comment
Add comment · Show 12 · 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 karlkevin · Apr 17, 2016 at 03:15 PM 0
Share

Thank you very much! :)

avatar image karlkevin · Apr 17, 2016 at 03:42 PM 0
Share

But also I have next problem. Atm it changes only one time picture, and if i dont leave scene, it wont change the picture again, when I press the second time. Any idea how to dix this?

avatar image Ali-hatem karlkevin · Apr 17, 2016 at 04:05 PM 0
Share

it work with me have you attached both images & what do you mean if i don't leave scene : reload the scene or other scene ?

avatar image karlkevin Ali-hatem · Apr 18, 2016 at 06:22 AM 0
Share

I mean that when I go to the scene where I have this button and after I have pressed it, it changes picture- works, changes "OnSprite" -> "OffSprite". But, when I press it again, nothing happens. It should change "OffSprite" -> "OnSprite", however, when I leave the scene after first change and come back to the scene where that button is and when I press it, then it changes "OffSprite" -> "OnSprite". Hope you undrestand, what I mean.

Show more comments
avatar image karlkevin · Apr 19, 2016 at 07:25 PM 0
Share

public void $$anonymous$$usiconoff() { PlayerPrefs.GetInt ("musicpic"); if (music == 0) { but.image.sprite = musicoff; } if (music == 1) { but.image.sprite = musicon; }

     if (music == 0) {
         AudioListener.pause = true;
         PlayerPrefs.SetInt ("music", 1);
         PlayerPrefs.Save ();

     } else if (music == 1) {
         AudioListener.pause = false;            
         PlayerPrefs.SetInt ("music", 0);
         PlayerPrefs.Save ();
     }

 
 }

So this is the code I got atm. Im going to add picture, which will maybe make it easier to undrestand.

If I press the button as on the upper row on the picture, it changes first time from on to off, if i press it again, nothing happens. If I leave the scene as on the lower row, and come back, it has changed the picture. I want it to change the picture infinite times, without leaving the scene.

alt text

button.jpg (57.0 kB)
avatar image Ali-hatem karlkevin · Apr 19, 2016 at 10:18 PM 0
Share

tomorrow i will cheek it it's 1 A$$anonymous$$ now good night

avatar image karlkevin Ali-hatem · Apr 20, 2016 at 07:55 AM 0
Share

Okay, seeya =)

Show more comments
avatar image daggertok · Mar 31, 2020 at 01:30 PM 0
Share

This should be the best answer

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

69 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does my jump button not work in some scenes 0 Answers

OnMouseDown work through UI elements 3 Answers

How to have get button display a raw image? 3 Answers

getcomponent cant find every button script in every gameobject 1 Answer

How to keep bool true even when other method try to set it off 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