How to change image of button when clicked?
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! :)
This hasn't really explained much as to what your problem is, what GUI systems you are using, etc. Please expand.
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 :).
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:
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!
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.
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?
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 ?
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.
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.
tomorrow i will cheek it it's 1 A$$anonymous$$ now good night
Your answer
Follow this Question
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