• 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
1
Question by IvayloDev · Aug 22, 2015 at 03:54 PM · uibuttontoggle buttonstatesbetween

How to change between two images on button click

Hello. I searched but didn't find any answer. I basically have 1 button for toggling between music on/off. I also want when the button is pressed to change the image to "music off" image and at the same time disable the music. I tried changing the image from the script but it didn't work.

Comment
Add comment
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

3 Replies

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

Answer by Wolfshadow · Aug 23, 2015 at 12:13 AM

I use:

 {
                     public bool Show = true;
 
             public GUITexture checkmark;
 
             public Texture2D trans;
     public static int showINT = 1;
 
 
     public Texture2D checkmark1;
 
     void Update ()
     {
         if (Show == false) 
         {
             showINT = 0;        
         }
 
         else 
         {
             showINT = 1;
         }
     
     }
 
     void OnMouseDown ()
     {
             
         if (Show == true)
         {
             checkmark.GetComponent<GUITexture>().texture=trans;
             Show = false;
             Debug.Log ("False");
             PlayerPrefs.SetInt("hide",showINT = 0);
         }
 
         //you are hidden, do not fear
         //do required stuff
         else
         {
             checkmark.GetComponent<GUITexture>().texture=checkmark1;
             Show = true;
             Debug.Log ("Show");
             PlayerPrefs.SetInt("show",showINT = 1);
         }
 
         // Great Scott! They've found us!
         //do required stuff
 
 
 
     }
 }


This code changes textures, but I use a transparent and normal texture. That's why it has hide vs. show. Just assign any two textures and a gui texture in the editor.

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
1

Answer by RafiXWPT · Aug 22, 2015 at 05:35 PM

You are probably doing something wrong. This is proper way to do it:

 public Sprite musicOnImage;
 public Sprite musicOffImage;
 GameObject yourimage;
 
 void Start() {
      yourimage = GameObject.Find("yourimage"); // You can find that refference in any way you want, its just example.
 }
 
 void ChangeMusicOnOffImage(bool on)
 {
      if(on)
           yourimage.GetComponent<Image>().sprite = musicOnImage;
      else
           yourimage.GetComponent<Image>().sprite = musicOffImage;
 }

It has to work cus I am using simillar code myself.

Next you have only connect that method with button, or add additional global variable to check if music should be off, idk, its up to you.

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
0

Answer by MeeWowGames · Aug 15, 2017 at 07:11 AM

It's a bit surprising this isn't directly supported by Unity's Toggle object, but fortunately there's an easy solution.

After creating your Toggle GameObject, add a new Image GameObject under Toggle/Background/ (next to the Checkmark GameObject it includes by default). Make a public Image variable in your script and link this new image to it, like this:

 public Image imageToggle_X;

Then, in your script's init function & in the Toggle's OnValueChanged handler, add this line:

 imageToggle_X.gameObject.SetActive(!toggleNotifications.isOn);

That way, the Toggle will always draw the background image, it'll draw the Checkmark Image when it's on, and your own custom Image will draw when it's off.

Enjoy!

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

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

button click() requires 2 clicks to work 1 Answer

UI Button is unclickable based on position in Hierarchy 1 Answer

Button mouseover only works when other canvas is active 0 Answers

UI Jump Button - I want to click on the button to jump but instead I jump when I click anywhere on the screen. 0 Answers

Upgrade Weapon Stats on Different Script 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