This article is Part 20 in a 25-Part Series.

Let’s start with the easiest of the Bonus Crates - Tank bonus crate which grants 1 extra live to the player.

Start by dragging and dropping the Sprite you have for Tank bonus crate into the hierarchy which Unity will help to create the Game Object for you. Call the Game Object 1up.Add a Box Collider 2D and ensure its boundary fills the entire crate’s image, then make it a trigger. Also, create and assign a new Layer called Powerups to the Game Object. You should also set its Order In Layer to 2 to ensure it gets rendered on top of all objects.

Adjust the sprite's Pixels Per Unit so that the Game Object fills the same amount of area as a tank.

Go to the Edit->Project Settings->Physics 2D and update the collision such that only PlayerTank can collide with PowerUps layer.

Coding the 1 up

All the Crates appearing on the screen has a blinking effect on them so we will create an abstract class script to hold the code for the blinking effect, then the script to code the individual effects of the different crates will inherit from it. The abstract class script will be called PowerUps. The code as below.

using UnityEngine;

public abstract class PowerUps : MonoBehaviour {
    private SpriteRenderer sprite;
    protected virtual void Start () {
        sprite = GetComponent<SpriteRenderer>();
        InvokeRepeating("Blink", 0, 0.1f);
    }
    void Blink()
    {
        sprite.enabled = !sprite.enabled;
    }
}
The code gets the SpriteRenderer component of the bonus crate and repetitively turn on and off the sprite renderer. The protected virtual at the Start Monobehaviour is to allow classes inheriting from PowerUps to call and modify the Start MonoBehaviour codes written in the abstract class(running the GetComponent from PowerUps won't work as the script attached to the bonus crate will be the script inheriting from PowerUps)

Now we start creating the effects of the bonus live. Create a script called OneUp in the 1up Game Object. Set the class to inherit from PowerUps. The full code for OneUp as below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OneUp : PowerUps
{
    protected override void Start()
    {
        base.Start();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        MasterTracker.playerLives++;
        GamePlayManager GPM = GameObject.Find("Canvas").GetComponent<GamePlayManager>();
        GPM.UpdatePlayerLives();
        Destroy(this.gameObject);
    }
}
The code starts by triggering base.Start which gets the code from its parent class PowerUps Start Monobehaviour to run. Then in the OnTriggerEnter2D, add one life to playerLives which is a static variable in MasterTracker. Then run the routine UpdatePlayerLives which will update the Battle Status Board of the number of lives the player now has. Once that is done, it will destroy the game object.

That’s all! We can test it already. Create a prefab of 1up by dragging it to the Project Window. Go to the Canvas Game Object and drag and drop the 1up prefab to the Bonus Crates under Game Play Manager (Script). We can also test the crate to not appear at silly positions effect by overflowing the gameplay area with Water tiles. We will also set the number of enemy tanks to spawn to 5 with a 100% chance of it being a bonus tank.

Testing successfully! We see that the crates only get spawned in areas where we can reach and also the effects(plus 1 life) is working! You can prefab the 1up now. Next post we will talk about the invincibility crate.

This article is Part 20 in a 25-Part Series.