Weird Coin Increase issue

There was a title “Wierd problem with the coins , plz help” , i had same issue with that discussion and solution did not work for me.

Issue was :

If I load into level 2 or 3 instantly instead of starting from level 1 the coin pickups work just fine. But if I for example start from level 1 (which I don’t have any coins in) and complete the level and then go over to level 2, I can pick up the coins and the coin disappears, the sound effect plays but the score doesn’t increase. The same thing happens if I start in level 2 and pick up the coins there the score increases as it should but then when I complete the level and pick up coins in level 3 it doesn’t work.

Plz help

Provided CoinPickup.cs :

I had same issue with that guy and my solution is :

  • Instead of reference GameSession and initialize it in Start method
  • Use FindObjectOfType().IncreaseScore(pointsValue); in OnTriggerEnter2D method

and if you want to look at , my CoinPickup.cs :

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class CoinPickup : MonoBehaviour

{

    [SerializeField] Vector2 pickupKick = new Vector2(-10f, 10f);

    [SerializeField] float delayTime = 0.3f;

    [SerializeField] float gravityScale = 1f;

    [SerializeField] AudioClip coinPickupSFX;

    [SerializeField] int coinPickupValue;

    Rigidbody2D rb2d;

    void Start()

    {

        rb2d = GetComponent<Rigidbody2D>();

    }

    private void OnTriggerEnter2D(Collider2D other)

    {

        if (other.tag == "Player")

        {

            PickupCoin();

        }

    }

    private void PickupCoin()

    {

        rb2d.gravityScale = gravityScale;

        rb2d.velocity = pickupKick;

        AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position, 0f); // 0f is volume

        Debug.Log("Picked coin tag : " + gameObject.tag);

        FindObjectOfType<GameSession>().IncreaseScore(coinPickupValue);

        StartCoroutine(DestroyCoin());

    }

    private IEnumerator DestroyCoin()

    {

        yield return new WaitForSecondsRealtime(delayTime);

        Destroy(gameObject);

    }

}

!! I added more functionality to my CoinPickup.cs , so if your code is not same as my code dont worry about it

Privacy & Terms