Adding coins to level 2 & 3

To be honest and I have been trying to figure it out. But I don’t know where to write the if-block.
:neutral_face:

You already have an if-block in your OnTriggerEnter2D method. Add another Debug.Log to that if-block to see if it gets executed.

Since “hi” got logged into your console, you know that OnTriggerEnter2D got called. However, just because OnTriggerEnter2D got called does not mean that the if-block got executed, too.

Thanks for your patience with me Nina.
It says:

UnityEngine.Debug:Log (object)
CoinPickup:OnTriggerEnter2D (UnityEngine.Collider2D) (at Assets/Scripts/CoinPickup.cs:13)

Great. I assume you tested the code in level 1 and level 2? If so, you can assume that the problem is not caused by your code.

Select the Main Camera game object in level 2. Does it have the “Main Camera” tag assigned in its Inspector? Is there an Audio Listener component attached to the Inspector?

You do not have any other cameras in your scene, do you? Since the PlayClipAtPoint method spawns a new AudioSource at Camera.main.transform.position, it might be that the sound does get played but you cannot hear it because it is too far away from the camera rendering your scene.

If your audio clip worked in level 1, check if there are any differences in level 2.

I have mai camera and state driven camera. Under state driven camera, I have Run, idle and ladder camera on all the levels.I have been starring at all the settings for two days now, without finding any differences. But I have also experienced that sometimes,es you look so hard, that you go blind from seeing what is wrong with the codes and settings sometimes.
The settings are all the same from level one, two and 3.


Why do you have an AudioSource attached to “Coin (1)”? Is “Coin (1)” a game object in level 1 (= the working level) or level 2 (= the broken level)?

What happens if you prefab the coin from the working level and drag the prefab into the broken levels? Does the new coin work?

Every time I drag the coin in to the scene of the level, it comes coin 1, 2 and 3. In the first level I have 3 coins and they all work. I drag from the prefabs. When I do the same in level 2 and 3, it doesn’t work.
Coin 1 is a gameobject in level 1.

Could you please upload your project to GitHub without the Temp and Library folder and share a link to the public repository here? I’d like to take a look into this.

Here is a tutorial video by Brackeys:

https://www.youtube.com/watch?v=qpXxcvS-g3g

Hi Nina, sorry in advance if I have done something wrong Now I feel small. I had to watch another tutorial video to understand it better. Here goes nothing:

Unfortunately, that’s not the complete project. The entire Assets folder with your scripts and scene(s) is missing. What was the problem with Brackey’s tutorial?

If creating a repo is too difficult, zip your project, upload it somewhere on the internet and share a link here. Do not upload the Library and/or Temp folders. My internet connection is fairly slow.

Okay, sorry. Let me see what I can do. I am not sure there is something wrong with Brackey’s tutorial. But he is on a Windows computer and I am on a Mac. So everything looks different. Even when I log on GitHub. I have bought a PC laptop, where I will do all the tutorials in the future. I just need to finish this session.

What exactly looks different? Actually, git/Github are platform independent. This is the first time I read that somebody was not able to create a repository with Brackeys tutorial. :confused:

You actually managed to create a repository. I was able to download your project. The only problem is that files/folders are missing.

If a git repo does not work for you, zip your project folder (without the Library and Temp folders). That’s a bit less convenient for me but we could make an exception. :slight_smile:

1 Like

Hi Nina, I have replied on the email I got from this reply with a zip file. Maybe that was wrong?
All The Best, Serafín

When I messed up game objects dealing with prefabs in earlier tutorial segments, it ended up being that I was setting up the game object instead of the prefab and forgot to apply the override changes from that object to the prefab. If this is something else I’d like to know what went wrong for you, but this sounds like the same issue I had setting up my enemies where one works and the rest don’t.

1 Like

What e-mail? If you created a repo or made a commit, it might be that GitHub sends you an e-mail. You won’t get an e-mail by me (or another Gamedev.tv teaching assistant).

I will check it out. Right now I think I have already try this. But I will look at it again.

OK, so confusing, because I looks like a normal e-mail and like I am getting the reply from you and that I can reply to you from there.

I uploaded the zip to my repo.
Once again I am grateful for your patience and help.
About the tutorial… On my screen github is black and instead of saying create it says new. Even GitHub Desktop looked different. Small variations like that. Even in Unity. I had to look for a long time for a setting that wasn’t in the same place as his was for me.
Once again, thank you thank you thank you!

All The Best, Serafín Ziquer Xavier and have a great weekend!

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:

OMG Nina! I have looked and looked, I swear that I checked it, but I guess I was blind in the end. The only thing I needed to do was to tag the player. I remember you even suggested that before and I remember that I checked it again after you asked me. My God! Sorry! And 1000 thanks for all your help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms