Need to play lock-on sound only once when crosshair placed onto enemy

Hi,

I have a script which adds the enemies within a trigger to an array, finds the closest one to the player and then places the crosshair onto them and fires in their direction. What I would like to do is play a ‘Lock-on’ sound when the crosshair is placed onto a new enemy. Now, the sound must only be played once when locking onto onto a new enemy. The issue is that in order to stay locked onto the enemy as they move, commands have to be placed into/executed from the Update() function of a script. I need a way to say that when targeting an enemy, play the sound once (so far the sound is playing constantly for the above reasons), so I’m not sure where to call this from. I’ve tried using flags out and inside of if- statements but this cannot be inside an Update() as this is called multiple times. I have a feeling I’m missing something very simple here :s

I have a script dedicated to sounds with a function called PlayLockOnSound() attached to the Player object which simply contains the command:

audio.PlayOneShot(lockOnSound);

The gist of the else statement inside the script’s Update is as follows, this is attached to a Box Collider(Is Trigger):

// Otherwise, the array must have something inside it (i.e enemies)
    else
    {
        for (int i = 0; i < enemyCounter; i++)
        {
            Debug.Log("***" + enemyArray*);*

// Access the CrosshairToggle script and Enable it
GetComponent().enabled = true;
// Assign the current enemy’s position to the global variable EnemyPosition
EnemyPosition = enemyArray*.transform.position;*
// Call the FindClosestEnemy() function to find hich of the enemies in the enemyArray (the tube)
// is the closest to the player.
FindClosestEnemy();
// Access the script’s EnemyPos variable and pass it the position of the enemy
GetComponent().EnemyPos = EnemyPosition;
// Find the BulletSPawner object
GameObject bulletDirObj = GameObject.Find(“BulletSpawner”);
// Access the object’s Shooter script and assign the value in bulletDirection to bulletDir here
Vector3 bullerDir = bulletDirObj.GetComponent().bulletDirection;
bool playOnce = false;
// WHAT TO DO HERE ???
if (!playOnce)
{
GameObject soundObj = GameObject.Find(“Player”);
soundObj.GetComponent().PlayLockOnSound();
playOnce = true;
}
}
}

I didn’t understand exactly what you’re doing in your script, thus I changed the logic: the closest enemy in enemyArray is found and compared to curEnemy - if different, the sound is played and curEnemy is updated. I also cached the script and object references at Start, because finding them over and over wastes a lot of time. Maybe the logic I used isn’t compatible to what you want to do, but hope the basic idea can be adapted to your case:

CrosshairToggle crosshairScript;
Shooter shooterScript;
GameObject player;
PlayerSounds soundScript;
GameObject curEnemy;

void Start(){
    crosshairScript = GetComponent< CrosshairToggle>();
    shooterScript = GameObject.Find("BulletSpawner").GetComponent< Shooter>();
    player = GameObject.Find("Player");
    soundScript = player.GetComponent< PlayerSounds>();
}

    ...
    // find the closest enemy in enemyArray:
    float distance = Mathf.Infinity;
    GameObject closest;
    foreach (GameObject enemy in enemyArray){
        float dist = (player.transform.position-enemy.transform.position).sqrMagnitude;
        if (dist < distance){
             closest = enemy;
             distance = dist;
        }
    }
    // if closest enemy found, and it's different from curEnemy...
    if (closest && closest != curEnemy){
        soundScript.PlayLockOnSound(); // play the lock sound
        curEnemy = closest; // update curEnemy
    }
    EnemyPosition = curEnemy.transform.position;
    // Access the CrosshairToggle script and Enable it
    crosshairScript.enabled = true;
    // Access the script's EnemyPos variable and pass it the position of the enemy
    crosshairScript.EnemyPos = EnemyPosition;
    // Access the object's Shooter script and assign the value in bulletDirection to bulletDir here
    Vector3 bullerDir = shooterScript.bulletDirection;
    ...