Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Abdul91 · Sep 09, 2014 at 11:37 AM · guitexture

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)

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

1 Reply

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

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

Comment
Add comment · Show 4 · 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 Abdul91 · Sep 10, 2014 at 04:46 AM 0
Share

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;

avatar image Jan_Julius · Sep 10, 2014 at 07:44 AM 0
Share

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;
                 }
avatar image Abdul91 · Sep 10, 2014 at 08:05 AM 0
Share

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 :)

avatar image Jan_Julius · Sep 10, 2014 at 08:53 AM 0
Share

Yeah, that should do the job perfectly fine :).

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

3 People are following this question.

avatar image avatar image avatar image

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


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