Coin System

We added a Coin system and random coin generation code.

p16

Here is our Coin class.

public class Coin : MonoBehaviour 

{

    public GameObject       Effect;

    public int                PointsToAdd = 1;

    public AudioClip       HitCoinSound;

    public void OnTriggerEnter2D (Collider2D other)

    {

        if (other.GetComponent<Player>() == null)

            return;

        if (HitCoinSound != null)

            AudioSource.PlayClipAtPoint(HitCoinSound, transform.position);

        GameManager.Instance.AddPoints (PointsToAdd);

        //Instantiate (Effect, transform.position, transform.rotation);

        FloatingText.Show (string.Format(“+{0}!”, PointsToAdd), “PointCoinText”new FromWorldPointTextPositioner (Camera.main, transform.position, 1.5f50));

        DestroyCoin();

    }

    public void DestroyCoin()

    {

        Destroy(gameObject);

    }

}

public class CoinSpawnManager : MonoBehaviour 

{

    public GameObject CoinPrefab;

    public GameObject TopRowMidPoint;

    public GameObject MidRowMidPoint;

    public GameObject BottomRowMidPoint;

    public float CoinsOffset;

    private BoxCollider2D _collider;

    private BoxCollider2D _camBounds;

    private GameObject _coinPrefab;

    private float _coinYpos;

    public void Start()

    {

        _collider = (BoxCollider2D)GetComponent<BoxCollider2D>();

    }

    public void OnTriggerEnter2D ( Collider2D other)

    {

        if (other.GetComponent<CameraBound>() == null)

            return;

        SpawnCoins();

    }

    public void SpawnCoins()

    {

        // spawn 5 coins in random row

        // random number 1, 2, 3

        Vector3 pos = transform.position;

        int row = Random.Range(15);

        if (row == 1)

        {

            _coinYpos = TopRowMidPoint.transform.position.y;

        }

        else if (row == 2)

        {

            _coinYpos = MidRowMidPoint.transform.position.y;

        }

        else if (row == 3)

        {

            _coinYpos = BottomRowMidPoint.transform.position.y;

        }

        else

        {

            // skip

            return;

        }

        pos.y = _coinYpos;

        for (int i = 0; i < 5; i++)

        {

            _coinPrefab = (GameObject)Instantiate(CoinPrefab, pos, transform.rotation);

            pos.x += CoinsOffset;

        }

    }

}

In order to not run out of memory we simply destroy tiles and coins after a certain time,

Following script is attached to tile prefab and coin prefab

public class DestroyGameObjectTimer : MonoBehaviour 

{

    public float DestroyTime;

    private float _destroyTime;

    public void Start()

    {

        _destroyTime = DestroyTime;

    }

    public void Update ()

    {

        if ( (_destroyTime -= Time.deltaTime ) <= 0)

            Destroy(gameObject);

    }

}

Thanks for reading.

if you want to check out some of our projects please subscribe to our YouTube channel:

You can check up our work in progress games here:

If you are looking for solutions to game development issues see our blogs here:

If you like our projects please sign up for our newsletter at the bottom of this Page.

 

Leave a Reply