Adding coins to level 2 & 3

Thanks. I downloaded the zip, and I was able to open the project. Solving the problem with the non-collectable coins took me a while but the fix was very simple: The player did not have the “Player” tag assigned in Level 2 and 3. I was looking at the assigned “Layer” and thought that was the tag until I logged the tag into the console. The console said “untagged” but the code needs:

if (other.tag == "Player" && !wasCollected)

Then I took a closer look at the Inspector and noticed my mistake.
image


However, I still cannot hear the sound when I collect a coin in level 2, even though the “One shot audio” game object appeared in the Hierarchy. When I disable the CinemachineBrain component on the Player, I am able to hear the sound. I did some research and found a thread by another student. For them, the fix was to set the “Update Method” of the CinemachineBrain to “Manual Update”.

image

Unfortunately, the camera will not move anymore with that setting.

I fixed that problem by moving the AudioListener to a non-moving game object: “Cameras”. I also added a new class: AudioListenerPosition.

image

The AudioListenerPosition simply returns the position of this component/game object. This is a more performant solution than calling FindObjectOfType whenever you want to play a sound clip.

using UnityEngine;

[RequireComponent(typeof(AudioListener))]
public class AudioListenerPosition : MonoBehaviour
{
    static AudioListenerPosition instance = null;
   
    void Start()
    {
        instance = this;
    }

    public static Vector3 GetPosition()
    {
        if (instance == null) { return Vector3.zero; }
        return instance.transform.position;
    }
}

And in the CoinPickup class:

Vector3 position = AudioListenerPosition.GetPosition();
AudioSource.PlayClipAtPoint(coinPickupSFX, position);

Did this fix it for you?


See also: