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

Randomize parameter in mixer by script

Discussion in 'Audio & Video' started by frank88188, Mar 23, 2016.

  1. frank88188

    frank88188

    Joined:
    Mar 19, 2016
    Posts:
    2
    Hi
    I'm working on a project and I want to randomize the pitch parameter on pitch shifter during the gameplay.
    What I'm trying to do is use Random.Range to generate a number and use SetFloat to move the parameter. But it didn't move at all. :(
    This is the code I have so far.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Audio;
    4.  
    5. public class randomize : MonoBehaviour {
    6.  
    7.     public AudioMixer masterMixer;
    8.     public float pitchLowRange = .5f;
    9.     public float pitchHighRange = 2.0f;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         float pitch = Random.Range(pitchLowRange, pitchHighRange);
    19.         masterMixer.SetFloat("laserPitch", pitch);
    20.     }
    21. }
    22.  
    Help me~
     
  2. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    164
    Looks like you are getting a new random pitch value and assigning to the exposed AudioMixer property in Update(), which means it's happening every frame - try updating it less frequently, like only when the laser sound is called to Play(), or with an InvokeRepeating(). Also check that the exposed property name on the pitch shift effect matches what you've got in script.
     
  3. frank88188

    frank88188

    Joined:
    Mar 19, 2016
    Posts:
    2
    Thank you
    It's working:)
     
    aihodge likes this.