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

Hello everyone

Discussion in 'Scripting' started by djlevana18, Jan 12, 2021.

  1. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    i have a code in which i want to decrease music by delta time like if you are more in the game you can hear less any idea how i can do this? this is my code for slider
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Music : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public AudioSource AudioSource; // Assign in editor/etc
    9.     public float MusicVolume = 1f;
    10.    
    11.      void Start()
    12.     {
    13.         AudioSource.Play();
    14.     }
    15.      void Update()
    16.     {
    17.         AudioSource.volume = MusicVolume;
    18.     }
    19.     public void UpdateVolume(float Volume)
    20.     {
    21.         MusicVolume = Volume;
    22.        
    23.        
    24.     }
    25.    
    26. }
    27.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Steps to success:

    start a timer at zero when the level begins

    count it up steadily (
    age += Time.deltaTime;
    )

    develop a function for volume, expressed as this age.

    Start perhaps with:

    Code (csharp):
    1. var volume = 1.0f / (1.0f + age / 10.0f);
    You can plug imaginary numbers into age and see that:

    age        volume
    0 1.0
    1 .909
    5 .667
    10 .5


    etc

    OR... put an
    AnimatorCurve
    object in your script and you can edit the curve yourself visually, then use .Evaluate() to look the age up and get a volume.
     
  3. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    something like this? but it does not decreasing valume automaticly
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Music : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public AudioSource AudioSource; // Assign in editor/etc
    9.     public float MusicVolume = 100f;
    10.    
    11.     public float Age = 1.0f;
    12.    
    13.      void Start()
    14.     {
    15.         AudioSource.Play();
    16.     }
    17.      void Update()
    18.     {
    19.         AudioSource.volume = MusicVolume;
    20.     }
    21.     public void UpdateVolume(float Volume)
    22.     {
    23.         MusicVolume = Volume;
    24.  
    25.        
    26.        
    27.     }
    28.     public void Volume()
    29.     {
    30.         var Volume = 1.0f / (1.0f + Age / 10.0f);
    31.         Age += Time.deltaTime;
    32.     }
    33.    
    34.    
    35. }
    36.  
     
  4. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    well it's updating now i call Volume in Update with Volume() but it does not affect music
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Follow the data and straighten things out a bit.

    You have a function called Volume and a local variable inside it called Volume.

    You have a function called UpdateVolume that accepts an argument called volume and then sets a class member called MusicVolume.

    That is a LOT going on... and i looks like there are at least two things NOT being updated. Nobody calls Volume() and nobody calls UpdateVolume()!

    I would delete ALL of it and just do this:

    Code (csharp):
    1. void Update()
    2. {
    3.   Age += Time.deltaTime;
    4.   AudioSource.volume = 1.0f / (1.0f + Age / 10.0f);
    5. }
    Done. When in doubt, DELETE code. Always.
     
    seejayjames likes this.
  6. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    woooooow that helped me a lot thank you man i have only 1 question so i start from age60 and untill i will not reach age 200 there is no changing in volume not that much do you know why is that?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Music : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     public AudioSource AudioSource; // Assign in editor/etc
    10.     public float MusicVolume = 100f;
    11.    
    12.     public float Age = 0f;
    13.    
    14.     void Start()
    15.     {
    16.         AudioSource.Play();
    17.      
    18.     }
    19.     void Update()
    20.     {
    21.         Age += Time.deltaTime;
    22.         AudioSource.volume = 1.0f / (1.0f + Age / 10.0f);
    23.     }
    24.     public void UpdateVolume(float Volume)
    25.     {
    26.         MusicVolume = Volume;
    27.  
    28.        
    29.        
    30.     }
    31.    
    32.    
    33. }
    34.  
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    As ALWAYS for Unity, run the game and select the audio source, look at the volume to verify it is actually changing.

    If it is changing too slowly, change the constants in your function.

    If it is not changing at all, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  8. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    oh i get i change value from 100 to 10 now in MusiC volume but why do i start from 60 AGE?
     
  9. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    man thank you sorry to bother you i'm just new so thank you a lot
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    DON'T make that Age public if you're the only thing touching it inside.

    By making it public, it could be changed by other code, or the Unity serializer.
     
  11. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    thank you a lot you saved me
     
  12. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    can you help me 1 one more time?
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Start a fresh post please, with an actually-useful title this time.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  14. djlevana18

    djlevana18

    Joined:
    Dec 11, 2020
    Posts:
    25
    okay