Sound to loop while collision, but not when no collision?

I have a situation where the player moves over an area to charge up. I have a sound effect which I can play for the charging, but it’s not straightforward to implement.

If I just play the sound effect when the player is over the area, it continually restarts. I need it to play and loop when over the area, but stop playing when the player is not over the area. Any ideas?

I haven’t messed with sounds very much, but if you have the “Loop” checkbox checked in the inspector for the audio file, wouldn’t you be able to use the Play method to start it in OnTriggerEnter (I assume that’s where your code is located), and once the audio files reaches the end, it should automatically loop because of the checkbox. When your character exits the area to charge up, you can call the Stop method in OnTriggerExit. Assuming the sound you want to play is attached to the object that contains the trigger zone, the code could look something like this:

using UnityEngine;
using System.Collections;

public class ChargeZone : MonoBehavior {
    AudioSource powerupSound = gameObject.GetComponent<AudioSource>();
    
    void OnTriggerEnter(collider other) {
        powerupSound.Play();
    }
    
    void OnTriggerExit(collider other) {
        powerupSound.Stop();
    }
}

Hopefully this helps.