Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating a music on/off button

Discussion in 'Scripting' started by Pixelated_Lagg, Apr 15, 2021.

  1. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    So I am trying to create a button to turn on/off music. I have created an empty object called "Music" with an audio source attached with the music. I also have a script for the button:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MusicVolume : MonoBehaviour
    6. {
    7.     AudioSource audioSource = null;
    8.    
    9.     void start()
    10.     {
    11.         audioSource = gameObject.GetComponent<AudioSource>();
    12.     }
    13.  
    14.     public void MenuMusicVolume()
    15.     {
    16.         if (audioSource.mute == false)
    17.         {
    18.             audioSource.mute = true;
    19.         }
    20.  
    21.         if (audioSource.mute == true)
    22.         {
    23.             audioSource.mute = false;
    24.         }
    25.     }
    26. }
    Heres the button:
    upload_2021-4-15_11-53-33.png
    Any help is greatly appreciated
     
  2. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    One issue is with your .mute property toggling logic. Assume that audioSource.mute == false and then consider what will happen, step-by-step, when you call your MenuMusicVolume method. This script will mute your audio source but never unmute it.
     
  3. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Some of the setup is off-screen but the way I imagine it seams legit. Are you experiencing any specific issues other then the one above?
     
  4. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I thought that if audioSource.mute == false was detected, it would make audioSource.mute = true. Then if it detected that audioSource.mute == true, it would make audioSource.mute = false.
     
  5. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Whenever I press the button, the script doesn't make audioSource.mute = true.
     
  6. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    That's exactly right and that's exactly the problem here :)
    Thing is the way you structured your conditions will make all this happen in a single frame / single call to MenuMusicVolume method.

    Let's say you sound is muted. You click your button. Then MenuMusicVolume method gets called. (audioSource.mute == false) conditions is evaluated to true. Line 18 is executed and sound get unmuted. Then the condition (audioSource.mute == true) gets evaluated to true since we just set the audioSource.mute value to true. Line 23 gets executed and we mute the sound. All that happened in 1/10 of a millisecond and so no sound ever played.

    Try this:
    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     public class MusicVolume : MonoBehaviour
    6.     {
    7.         AudioSource audioSource = null;
    8.      
    9.         void start()
    10.         {
    11.             audioSource = gameObject.GetComponent<AudioSource>();
    12.         }
    13.    
    14.         public void MenuMusicVolume()
    15.         {
    16.             audioSource.mute = !audioSource.mute;
    17.         }
    18.     }
    Further reading:
    http://faculty.cs.niu.edu/~hutchins/csci473/control.htm
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else
    https://docs.microsoft.com/en-us/do...-logical-operators#logical-negation-operator-
     
  7. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Sounds like the issue is with the button setup - the off-screen part. Make sure that when you click your button then MenuMusicVolume method actually gets called. Use either debugger or Debug.Log for that test.
     
  8. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Ok, now I see what you were saying.
     
  9. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I can confirm that the button calls the MenuMusicVolume method and the music still doesn't get muted.
     
  10. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I also keep getting this error, even though I have the correct script as a component of my object.
    NullReferenceException: Object reference not set to an instance of an object
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The error will have the specific script and line number, which is the important information. You go to that line number, see what reference types are on that line. If more than 1 are on that line, add debugging ahead of it to determine which is null. Once you know exactly what is null, then you can start to investigate why it is null, once you know why it is null, then you can start to work on making it not null before trying to use it.

    This is a type of error you have to get good at fixing, because it is extremely common, and always resolved using the same process.
     
  12. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Best guess is that you're getting NullReferenceException on line: audioSource.mute = !audioSource.mute;

    Reason would be that we fail earlier at line: audioSource = gameObject.GetComponent<AudioSource>();

    This line tries to find a component of type AudioSource on this GameObject and assigns it's reference to audioSource variable.
    https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

    If the component is not there then audioSource remains null value and causes the exception down the line.
    All this will happen unless both the AudioSource component we're trying to mute and our MusicVolume component are both attached to the same GameObject.
     
  13. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I have the script attached to the same object that has the audio source.
     
  14. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Heres the attached script in case I attached it wrong:
    upload_2021-4-15_13-35-9.png

    upload_2021-4-15_13-35-54.png
     
  15. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    OK I think the screenshots confirm my diagnosis. Your Music GameObject has following components: Transform, AudioSource, ButtonTeleportation. However it does not have the MusicVolume component. It should!

    EDIT: One sec. I can see at least one more issue here. will edit here further.

    EDIT2: OK so do this. 1. Move your MusicVolume component (wherever it is now) to your Music GameObject. 2. In the Button inspector in OnClick section set it up such that button click calls the MusicVolume.MenuMusicVolume method of our Music GameObject.
     
    Last edited: Apr 15, 2021
  16. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I put the code from the Music Volume script into my Button Teleportation script (the script I use for all the other menu buttons) to make it more convenient for myself.
     
  17. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    OK this is getting ridiculous. I don't have enough information to go on and the information I do have keeps changing. Post the whole thing as it is now.
     
  18. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I set it up it still does not mute:
    upload_2021-4-15_14-2-0.png

    upload_2021-4-15_14-2-44.png
     
  19. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    That's not nearly enough for me to go on. I need the Hierarchy, Inspector for the GOs in Hierarchy, the scripts in full and who knows what else. Best if you publish a git repo for entire project or a package with a self-contained part of the project or something for me to have a look at and test.
     
    Joe-Censored likes this.
  20. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
  21. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Exception is thrown in MusicVolume component. As discussed before it will do that because it is not attached to the same GameObject that has AudioSource component - Music GameObject.

    The Music GameObject does have a ButtonTeleportation component but this component is not the one throwing the exception.

    The MusicVolume.MenuMusicVolume() method gets called somehow but that setup is not included in the screenshots. Perhaps you are clicking the wrong button.
     
  22. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    That error is from hours ago when I had the other script connected - I probably shouldn't have included it. The most recent errors are:
    upload_2021-4-15_14-37-36.png
     
  23. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
  24. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
  25. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    It wouldn't. Compiler catches syntax errors. This is not a syntax error. This is a human error of not understanding the underlying technology. The code works. It just doesn't do what you wanted it to do.
     
  26. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Understandable. Thanks for helping me :)
     
    Joe-Censored likes this.