Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

   public void RandomizeSfx (params AudioClip[] clips)
   {
    //Generate a random number between 0 and the length of our array of clips passed in.
    int randomIndex = Random.Range(0, clips.Length);

    //Choose a random pitch to play back our clip at between our high and low pitch ranges.
    float randomPitch = Random.Range(lowPitchRange, highPitchRange);

    //Set the pitch of the audio source to the randomly chosen pitch.
    efxSource.pitch = randomPitch;

    //Set the clip to the clip at our randomly chosen index.
    efxSource.clip = clips[randomIndex];

    //Play the clip.
    efxSource.Play();
   }