Forum rules - please read before posting.

Music and SFX off icon change

In my options menu, I have sliders for Music and SFX volume with nice little images of sound and SFX next to them. When I move the slider to 0, I would like these icons to change into their "Off" versions. I know how to do this with Menu-Change State-Show/hide menu element actions, but I don't know how to check the current music/sfx volume value (I just need to detect if it equals 0 or not). I could not find any Action i could use for this. Is there any, or would this require a script or another approach?

Comments

  • It's a bit of a workaround, but you can do this by using a Global Float variable, linked to Options Data, to control the volume instead of the built-in value.

    Create a new such variable, link it to the Slider, and then have the Slider's "ActionList on change" asset run an Save: Set option Action that sets the volume to the variable's value. To do this, define a Float parameter in the ActionList, and use the ActionList: Set parameter Action to transfer the variable's value to the parameter.

  • edited July 2024

    Thanks, Chris, it works!

  • I am just having this stupid issue when the Off graphic does not show up sometimes and remains inactive. I have the exact same setting for Music and SFX, one works, and the other does not. The Is Visible box is unchecked for these off graphics in the menu element settings. At first both worked, then something changed even though i didn't change anything.

    This is my AL:

  • There's likely a difference somewhere, even if it's slight.

    Rather than using an Equal To check, though, try using a Less Than 0.001 check (i.e. less than a very small number).

    The ActionList looks like it can be run at any time. What are its Properties, and what is the value of the SetSFX variable at the time?

    You can use the ActionList: Comment Action on the end of each branch to work out which is being run. Adding the following will also print out the parameter's value:

    Param value: [param:0]
    
  • The action list works and the menu elements swap at the right moments, but the Off version just does not appear - as in the image is missing, although it shouldn't. The On version appears and disappears properly When I turn on the Settings menu (and when using ActionList: Comment), I also get this warning:

    Cannot find UnityEngine.UI.Image for menu element SFXGraphicOff in Canvas Settings.

    I have a suspicion it is caused by the "Is visible" being unchecked in the menu settings. I noticed, that sometimes this in turn makes the element inside the menu prefab turned off completely. Is this perhaps some unity bug?

    The parameter SetSFX does not change on runtime, which also worries me (but the global SFX Volume does).


  • Making a Graphic element invisible will, currently, disable the entire GameObject - but that shouldn't affect its ability to become visible again.

    Can you share the full warning message you mentioned, as well as a screenshot of the SFXGraphicOff's full Inspector?

  • When i run the game, making the element invisible also keeps turning off the objects in the prefab (although they were originally set to on). And only sometimes. Also, the source image in the prefab keeps turning to none (but the other objects in the prefab that are not affected by the visible/invisible action keep their source images).


  • I also tried to attach the Remember Visibility component, but it had no effect.

  • edited July 2024

    The next AC update will allow you to configure whether UI Graphic elements disable their associated Image component - or their entire GameObject - when hidden.

    How are the objects arranged in the Hierarchy? There should be no difference if SFXGraphicOff has no children.

    If you don't have this issue with Music, however, there may be some other factor at play.

  • I have the issue with both Music and SFX, I'm just showing it on one for simplicity.

    This is the hierarchy:

  • I have the issue with both Music and SFX, I'm just showing it on one for simplicity.

    I am just having this stupid issue when the Off graphic does not show up sometimes and remains inactive. I have the exact same setting for Music and SFX, one works, and the other does not.

    I was taking this to mean that Music works and SFX does not - is it instead the case that that the "On" works, and "Off" does not?

    I can't tell the cause of this from the details, but a quick fix would be to remove the On/Off elements from the Menu Manager, and instead attach a custom script (one instance for SFX, another for Music), to show/hide the on/off buttons automatically:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class SetOnOffGraphics : MonoBehaviour
    {
    
        public Image onImage, offImage;
        public string variableName;
    
        void Update ()
        {
            bool isOn = (GlobalVariables.GetVariable (variableName).FloatValue >= 0.01f);
            onImage.enabled = isOn;
            offImage.enabled = !isOn;
        }
    
    }
    
  • edited July 2024

    I was taking this to mean that Music works and SFX does not - is it instead the case that that the "On" works, and "Off" does not?

    Yes, the issue is the same for both Music and SFX, the On graphic shows, but instead of the Off graphic, there is empty space.

    I can't tell the cause of this from the details, but a quick fix would be to remove the On/Off elements from the Menu Manager, and instead attach a custom script (one instance for SFX, another for Music), to show/hide the on/off buttons automatically:

    I am not sure if I understand correctly. Should I attach this script to the whole Prefab or specifically to the SFXGraphic and MusicGraphic objects (they are not buttons, just images) (and I should delete the SFXGraphicOff and MusicGraphicOff objects)?
    In any case, when I try to do any of these, I get an error: Can't add script component 'SetOnOffGraphics' because the script class cannot be found.

  • Don't remove any objects from the UI - but you'd need to remove the Graphic elements from the Menu.

    The script must be attached twice - one for SFX, one for Music. Anywhere on your prefab is fine, as the fields are all assigned in the Inspector.

    You'll need to name the script file SetOnOffGraphics.cs for it to compile.

  • Got it, i had an extra space at the end of the name of the script... It works perfectly now, thank you!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.