Anyone any Idea why the code from the lecture works but mine doesn’t? The score doesn’t get updated while playing but he will update the score on the next playtest. Ricks version works fine.
My Version with the GameSession referenced in a SerializedField and it should behave the same way, at least i think so :
public class CoinPickup : MonoBehaviour
{
[SerializeField] GameSession gameSession;
[SerializeField] AudioClip coinPickupSFX;
[SerializeField] int coinValue = 100;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
IncreaseScoreCount();
AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position, 0.5f);
Destroy(gameObject);
}
}
void IncreaseScoreCount()
{
gameSession.IncreaseGameScore(coinValue);
}
}
Ricks version from the course where he searches for GameSession via FindObjectOfType:
[SerializeField] AudioClip coinPickupSFX;
[SerializeField] int coinValue = 100;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
FindObjectOfType<GameSession>().IncreaseGameScore(coinValue);
AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position, 0.5f);
Destroy(gameObject);
}
}
Pictures of the code:
Mine:
Ricks: