Referencing Oddities

Greetings. I’m new to this whole scripting thing, but I’m learning bit by bit. I’m currently messing around with referencing and, while I have a basic understanding of how it works, my current script produces curious results. Script using the reference:

using UnityEngine;
using System.Collections;

public class ObjectManager : MonoBehaviour {

    public PlayerController playerController; //script I am referencing

    AudioSource audioSource; //an audio source is attached to the game object holding this script
	
	void Start () {
        audioSource = GetComponent<AudioSource>();
    }
	
	
	void Update () {
        playAudio();
    }

    void playAudio(){
        if (playerController.count == 5){
            audioSource.Play();
        }
    }
}

And here is the relevant section of PlayerController in regards to my “count” variable:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public int count;

    void Start(){     
        count = 0;
    }

 void OnTriggerEnter2D(Collider2D other){
        if (other.gameObject.CompareTag("Sprite")){
            other.gameObject.SetActive(false);

            count++;
        }
}    

And of course, I dragged the object with the PlayerController script attached into the ObjectManager script component via Unity’s inspector. Count is properly incrementing according to the public variable’s inspector window. However, this code isn’t working as expected. Here are the problems I have encountered:

  1. The Audio clip will not play unless “count” is one integer greater than the condition specified in the “if” statement. So “if (playerController.count == 5)” will not be true until score is 6 for some odd reason.

  2. If the condition is set to “if (playerController.count > ‘any negative number’)”, “if (playerController.count == 0)”, or “if (playerController.count < ‘any positive number’)”, the audio clip will play immediately as expected, but only a fraction of it for a split second. Any increase in count does nothing after this point.

  3. The condition “if(playerController.count > ‘0 or any positive number’)” does nothing when the condition is met.

Any idea why this code is responding so strangely?

You’re calling audioSource.Play() in Update(), which is called every frame.

So, as soon as playerController.count == 5, you’re playing your audio source 60 times a second (let’s say for the sake of argument your game is running at 60FPS). Each new Play() restarts the audioclip, which is why you can’t hear it - there’s probably a slight silence at the beginning of your clip. But as soon as the condition is no longer true (i.e. when playerController.count==6), it doesn’t restart the audio, which means you get to hear out the whole of the clip that was last called.

It’s unclear what desired behaviour you actually want, but it might help you understand what is happening if you did something like this, to only restart the audio if it’s not already playing:

     if (playerController.count == 5 && !audio.isPlaying){
         audioSource.Play();
     }