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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Change float by using one slider ................ not access to my script

Discussion in 'Scripting' started by Bobindiana66, Aug 9, 2021.

  1. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Hi!
    I have a matter with the using of parameters of slider UI.
    In my script i want to change the value of dbMulti variable.


    I have just find the instruction ChangeSliderValue but now what i have to code to return this paramter in the slider. Don't recognize the code.
    I have set the script on the slider but no way to catch the variable Dbmulti, i don't understand the logic to catch the value.
    To set a light or a sound it is easy but when you wnt to change one variable float off a script how do you manage to succed ?
    Thanks.
    Code (csharp):
    1.  public void ChangeSliderValue () {
    2.  
    3.         float _dbMultiValue = _dbMultiValue + Slider.value * 0.01f;
    4.  
    5.  
    6.     }
    7.  
    8.     public float _dbMulti = 50;
     
    Last edited: Aug 9, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    I'll be honest, I don't know what you're trying to do.

    Can you better explain what you want to accomplish?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You have two variables above.

    Line 3 should probably ONLY be:

    Code (csharp):
    1. _dbMulti = Slider.value;
     
  4. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Thanks Kurt,
    how do i manage to link the slider value current with the value in the script is running, at start value is 50.
    The different options in the UI slider are not easy to navigate to obtain the good item you can choose.Can i take the new item in the menu of UI slider.
    If i put your line where i can link this value in the script Audiomanager, not easy to explain.




    My problem is to link my slider with this script and change the value of dB .
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class AudioManager : MonoBehaviour
    8. {
    9.     //public float _dbMulti = 50;
    10.      _dbMulti = Slider.value;
    11.     // first value is a "sample colums" and each of column have sample data
    12.     // it's used for making nice geometric stuff
    13.     public static float[][] _bandVolumes;
    14.  
    15.     private AudioSource _audioSource;
    16.  
    17.     public static int SAMPLE_COUNT = 1024;
    18.     public static int SAMPLE_COLUMS = 100;
    19.  
    20.     private List<float> _bands;
    21.  
    22.     void Start()
    23.     {
    24.         // creating range of bands, they should work flexible
    25.         _bands = new List<float>()
    26.         {
    27.             20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000
    28.         };
    29.  
    30.  
    31.         _audioSource = GetComponent<AudioSource>();
    32.  
    33.         _bandVolumes = new float[SAMPLE_COLUMS][];
    34.  
    35.         for(int i =0; i< SAMPLE_COLUMS; i++)
    36.         {
    37.             _bandVolumes[i] = new float[_bands.Count - 1]; // -1 beause bands are "from,to" like from 20 to 50
    38.         }
    39.  
    40.     }
    41.  
    42.     void Update()
    43.     {
    44.         // copying last values level "up"
    45.         for(int i = SAMPLE_COLUMS - 1; i>0; i--)
    46.         {
    47.             Array.Copy(_bandVolumes[i - 1], _bandVolumes[i], _bands.Count - 1);
    48.         }
    49.  
    50.         // reading current samples
    51.         float[] samples = new float[SAMPLE_COUNT];
    52.         _audioSource.GetSpectrumData(samples, 0, FFTWindow.BlackmanHarris);
    53.  
    54.  
    55.         float[] bandVolumes = new float[_bands.Count - 1];
    56.         for (int i = 1; i < _bands.Count; i++)
    57.         {
    58.             float db = BandVol(_bands[i - 1], _bands[i], samples) * _dbMulti;
    59.             bandVolumes[i - 1] = db;
    60.             // Debug.Log(i.ToString() + " " + db);
    61.         }
    62.  
    63.         _bandVolumes[0] = bandVolumes;
    64.     }
    65.  
    66.     public static float BandVol(float fLow, float fHigh, float[] samples)
    67.     {
    68.         float hzStep = 20000 / SAMPLE_COUNT;
    69.  
    70.         int samples_count = Mathf.RoundToInt((fHigh - fLow) / hzStep);
    71.  
    72.         int firtSample = Mathf.RoundToInt(fLow / hzStep);
    73.         int lastSample = Mathf.Min(firtSample + samples_count, SAMPLE_COUNT - 1);
    74.  
    75.        
    76.         float sum = 0;
    77.         // average the volumes of frequencies fLow to fHigh
    78.         for (int i = firtSample; i <= lastSample; i++)
    79.         {
    80.             sum += samples[i];
    81.         }
    82.         return sum;
    83.     }
    84. }