So I was having trouble getting the score to go up despite following what everything Rick was doing. I realized after 2 hours of troubleshooting that it was because of my use of the word “clip” as my variable in CoinPickup.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinPickup : MonoBehaviour
{
[SerializeField] AudioClip clip;
[SerializeField] int pointsForCoinPickup = 100;
bool wasCollected = false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" && !wasCollected)
{
wasCollected = true;
FindObjectOfType<GameSession>().AddToScore(pointsForCoinPickup);
AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
Destroy(gameObject);
}
}
}
My questions are:
- When creating a variable name, how do I check that there aren’t any operators or method names associated with the variable I’m creating so that I do not make the same mistake again?
- I understand that I’ve made a mistake (and the code worked when I fixed it to using variable name “coinPickupSFX” like Rick did), but this is a variable relating to sound. How does this hamper counting & collection of score in any way?? In my code the line "AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position); " is AFTER the FindObjectOfType code so I don’t see how it could impact the code.