- Home /
Unity changing GuiTexutre results null texure.
Hi, I am a very newb to unity like less than a month. I am having a problem changing guitexture of a guitexture. I have seen alot of questions and answers and have tried them. What I am doing is I have is a GuiTexture object referenced from my scene to the script. I have this piece of code in C#:
public GUITexture Sound;
void somefunction() { Sound.texture = Resources.Load("mute") as Texture2D; // tried using as Texture too }
When somefunction() is run, the button disappears - meaning the guitexture is set to none, I checked it while in play mode through inspector.
Any help would be appreciated, thnx in advance.
Also a side question, any difference between using sprites or guiTexture as buttons ? (Bearing in mind the different resolutions of the screens)
Answer by Jan_Julius · Sep 09, 2014 at 12:02 PM
Here you can see what the code does:
**http://gyazo.com/ce457f1e8e29f9ff5776e0a9288b6c90**
I'm not sure what you're doing but I'm assuming that you're having a speaker icon that gets switched to a muted one when there is no volume playing? I've written a script like such before and I will happily give you the code for it.
First you will need to have everything done before you actually can switch the 2 textures with one another, with this I mean is that you pre-add the 2 textures to the script in the inspector before even executing the code (if you haven't)
Add these 2 variables
public Texture mutedIcon;
public Texture unmutedIcon;
And for my own code I had a slider which you could slide which would also change the icon according to how loud the sound/volume was if you're after that add this variable
public float hSliderValue = 0.0F;
Additional variables: Boolean to determine wether the music was muted or not
public bool mutedMusic = false;
I haven't done anything with resolution so you might have to change some of the code down here. The OnGUI:
void OnGUI(){
if(GUI.Button (new Rect(40, 0, 40, 40), icon)){
mutedMusic = mutedMusic ? music.mute = false : music.mute = true;
hSliderValue = mutedMusic ? 0 : 10;
}
//wether the slider was on 0 or if the music is muted
if(mutedMusic || hSliderValue == 0){
icon = mutedIcon;
mutedMusic = true;
}
//if volume is higher than 0.1 basically
if(hSliderValue >= 0.1f){
icon = unmutedIcon;
music.mute = false;
mutedMusic = false;
}
//the slider that lets you control the volume
hSliderValue = GUI.HorizontalSlider(new Rect(0, 40, 100, 30), hSliderValue, 0.0f, 10.0f);
AudioListener.volume = hSliderValue/10.0f;
}
I've written the code quite some time ago so there might be some errors in it, if you need any more help you can ask, if not you can put this as an answer to your question and potentionally add a comment to it to make things clear that I haven't made clear in my code x).
Cheers!
[1]: http://gyazo.com/ce457f1e8e29f9ff5776e0a9288b6c90
Thank you mate, this is exactly what I needed, although I am not familiar with OnGUI function and what you are doing in it but your public Texture mutedIcon; public Texture unmutedIcon; definitely worked for me.
Just to be clear for any newb like me reading, I just adding those lines in my script, attached the images I want to be used with them from inspector. And my Sound.texture = Resources.Load("mute") as Texture2D; Changed To: Sound.texture = mutedIcon; or Sound.texture = unmutedIcon;
Don't worry! I'm a newb myself too just experiment a little and you can get a lot of things done, if I can't get something done I end up here too.
I believe that should work correctly, yes I'm not a 100% positive but if there is something that doesn't work correctly you can always just post again.
Read this again because I think this is what you're after.
//wether the slider was on 0 or if the music is muted
if(muted$$anonymous$$usic || hSliderValue == 0){
icon = mutedIcon;
muted$$anonymous$$usic = true;
}
//if volume is higher than 0.1 basically
if(hSliderValue >= 0.1f){
icon = unmutedIcon;
music.mute = false;
muted$$anonymous$$usic = false;
}
I actually just wanted to change texture of my GuiTexture element when I detect mouse press/ Tap on it. So, I was not sure whether if I should do all this in OnGui method. I just changed my texture to muted and unmuted texture on button tap for which ever case was true :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Triggering Main Camera Script with GUITexture 1 Answer
Animating GUITexture scale 0 Answers
How to return from the last case to the first 1 Answer
Problem with GUITexture.color.a 2 Answers