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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sound Script Error

Discussion in 'Scripting' started by Harardin, Aug 13, 2015.

  1. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Hello to everyone I take script from this question: http://answers.unity3d.com/questions/11486/footstep-sounds-when-walking.html and modify it a little but in the part of Script audio.clip = concreteSound[Random.Range(0, concreteSound.Lenght)]; the “Length” make an Error does not contain a definition. Can some one please help me to fix it.

    Here is the full Script:

    Code (CSharp):
    1. public class footStepSoundControll : MonoBehaviour {
    2.     public CharacterController controller;
    3.     public AudioClip concreteSound;
    4.     private bool step = true;
    5.     float audioStepLengthWalk = 1f;
    6.     // Use this for initialization
    7.     void Start () {
    8.         controller = GetComponent<CharacterController>();
    9.     }
    10.     void OnControllerColliderHit (ControllerColliderHit hit)
    11.     {
    12.         //Debug.Log("Controller hit the ground");
    13.         if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true )
    14.         {
    15.             WalkOnConcrete();
    16.         }
    17.     }
    18.     IEnumerator WaitForFootSteps(float stepsLength)
    19.     {
    20.         step = false;
    21.         yield return new WaitForSeconds(stepsLength);
    22.         step = true;
    23.     }
    24.     public void WalkOnConcrete ()
    25.     {
    26.         audio.clip = concreteSound[Random.Range(0, concreteSound.Length];
    27.             audio.volume = 1f;
    28.             audio.Play();
    29.             StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
    30.             Debug.Log("audio play");
    31.     }
    32.    
    33. }
    Alsow if use script like this it plays sound but only onese when button is pressed, and ones when button is Up

    Code (CSharp):
    1.     public void WalkOnConcrete ()
    2.     {
    3.         audio.clip = concreteSound; //[Random.Range(0, concreteSound.Length];
    4.             audio.volume = 1f;
    5.             audio.Play();
    6.             StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
    7.             Debug.Log("audio play");
    8.     }
    9.    
    10. }
     
  2. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    You forgot the last parenthesis inside the array.

    Code (csharp):
    1. audio.clip= concreteSound[Random.Range(0, concreteSound.Length)];
     
  3. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    "Length" cause an error I don't know why. I wrote about at the start of my post.
     

    Attached Files:

    • 123.png
      123.png
      File size:
      128.9 KB
      Views:
      753
    Last edited: Aug 13, 2015
  4. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    Okay, try casting it to an int, Random.Range returns a float. Array indexes are ints.
    Code (csharp):
    1. audio.clip = concreteSound[(int)Random.Range(0, concreteSound.Length)];
    Try that Post results if you get an error.
     
  5. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Just tryed still does not contain a definition to a Length, same error.

    If you interest I used as a reference for this Script script from this question, its in the end of a question at a firs page : http://answers.unity3d.com/questions/11486/footstep-sounds-when-walking.html
     
  6. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    http://docs.unity3d.com/ScriptReference/AudioClip-length.html

    In regards to audio clips, length is spelled with a lowercase L.

    Also, if you are trying to get a random clip to be selected from an array of audio clips, make sure that your AudioClips variable is actually an array.

    Code (csharp):
    1. public AudioClip[] concreteSound;
    And there needs to be indexes in the array for the script to call. At the moment, there is nothing in the array.

    I'm not completely sure what you're wanting to do. Are you trying to play just a single sound? Or grab from a collection of random Concrete sounds?
     
  7. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Also, if you are trying to get a random clip to be selected from an array of audio clips, make sure that your AudioClips variable is actually an array.

    Code (csharp):
    1. public AudioClip[] concreteSound;
    And there needs to be indexes in the array for the script to call. At the moment, there is nothing in the array.

    I'm not completely sure what you're wanting to do. Are you trying to play just a single sound? Or grab from a collection of random Concrete sounds?[/QUOTE]

    Yeah just found it, fixed the error, but when walking sound still play only once when start walking, and once when stop walking.
     
  8. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    I try to play walk sound when walking. like one sound on a one step of a char controller.
     
  9. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    If you stell interested I can achieve sond playng right on different surface, but with JS what I just copy.

    Still don’t know why C# script doesn’t work properly this two scripts are prety simmilar.

    One day when I learn code greater then I know it now I will translate it to C# but dor now I can achive only JS working version.

    Code (JavaScript):
    1. var stepWood:AudioClip[];
    2. var stepMetal:AudioClip[]; // fill this array with the sounds at the Inspector
    3. var stepConcrete:AudioClip[];
    4. var footSource1 : AudioSource;
    5. var footSource2 : AudioSource;
    6. var delayBeforeSteps : float = 0.20;
    7. var delayBetweenSteps: float = 0.45;
    8. var audioStepLength : float = 0.60;
    9. var groundType : int;
    10. var isPlayerWalking : float = 0.0;
    11. var footAudioRandomness = 0.1;
    12. var soundEffectPitchRandomness = 0.05;
    13. function Update(){
    14.     //Check to see if the player is walking by checking for input from the walking keys
    15.     if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
    16.         //if those keys are pressed, the player must be walking...
    17.         isPlayerWalking += Time.deltaTime;
    18.     }
    19.     else{
    20.         //if those keys aren't pressed, the player can't be walking.....
    21.         isPlayerWalking = 0;
    22.     }
    23. }
    24. function Start(){
    25.     var aSources = GetComponents(AudioSource);
    26.     footSource1 = aSources[0];
    27.     footSource2 = aSources[1];
    28.     while(true){
    29.         if (isPlayerWalking >= 0.20 && groundType == 1){
    30.             yield WaitForSeconds(delayBeforeSteps);
    31.             footSource1.clip = stepMetal[Random.Range(0, stepMetal.length)];
    32.             footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    33.             footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    34.             footSource1.Play();
    35.             if(isPlayerWalking == 0){
    36.                 yield;}
    37.             else{
    38.                 yield WaitForSeconds(delayBetweenSteps);
    39.                 footSource2.clip = stepMetal[Random.Range(0, stepMetal.length)];
    40.                 footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    41.                 footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    42.                 footSource2.Play();
    43.                 yield WaitForSeconds(audioStepLength);
    44.                 if(isPlayerWalking == 0){
    45.                     yield;}
    46.             }
    47.            
    48.         }
    49.         else if ( isPlayerWalking >= 0.20 && groundType == 2){
    50.             yield WaitForSeconds(delayBeforeSteps);
    51.             footSource1.clip = stepWood[Random.Range(0, stepWood.length)];
    52.             footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    53.             footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    54.             footSource1.Play();
    55.             if(isPlayerWalking == 0){
    56.                 yield;}
    57.             else{
    58.                 yield WaitForSeconds(delayBetweenSteps);
    59.                 footSource2.clip = stepWood[Random.Range(0, stepWood.length)];
    60.                 footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    61.                 footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    62.                 footSource2.Play();
    63.                 yield WaitForSeconds(audioStepLength);
    64.                 if(isPlayerWalking == 0){
    65.                     yield;}
    66.             }
    67.         }
    68.         else if ( isPlayerWalking >= 0.20 && groundType == 3){
    69.             yield WaitForSeconds(delayBeforeSteps);
    70.             footSource1.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
    71.             footSource1.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    72.             footSource1.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    73.             footSource1.Play();
    74.             if(isPlayerWalking == 0){
    75.                 yield;}
    76.             else{
    77.                 yield WaitForSeconds(delayBetweenSteps);
    78.                 footSource2.clip = stepConcrete[Random.Range(0, stepConcrete.length)];
    79.                 footSource2.volume = Random.Range(0.4 - footAudioRandomness, 0.4 + footAudioRandomness);
    80.                 footSource2.pitch = Random.Range(1.0 - soundEffectPitchRandomness, 1.0 + soundEffectPitchRandomness);
    81.                 footSource2.Play();
    82.                 yield WaitForSeconds(audioStepLength);
    83.                 if(isPlayerWalking == 0){
    84.                     yield;}
    85.             }
    86.         }
    87.    
    88.         else{
    89.             yield;
    90.         }
    91.     }
    92. }
    93. function OnControllerColliderHit (hit : ControllerColliderHit){
    94.     if (hit.gameObject.tag == "metalFloor"){
    95.         groundType = 1;
    96.         print("Metal");
    97.     }
    98.     else if (hit.gameObject.tag == "woodFloor"){
    99.         groundType = 2;
    100.         print("Wood");
    101.     }
    102.     else if (hit.gameObject.tag == "Untagged"){
    103.         groundType = 3;
    104.         print("Concrete");
    105.     }
    106. }