C# Creating a bar that decrements but can be incremented when pieces are matched

Hello I am working on a 2D match3 game and was wanting to have a bar that starts at 10 for example and decrements down but if a player matches something for it to increment. I am still learning coding, if anyone would be able to help me with this it would be greatly appreciated. I know how to reference when pieces are matched I’m more concerned about how to create a bar and have it decrease and increment.

Thanks in advance!

The way i would go to do is is with IMGUI ( Immediate Mode Graphical User Interface). IMGUI is a way to create run-time GUI through code. What I’d reccomend you is to have a script attached to your camera that has some GUI functions (I do it in the camera just as reminder where my GUI scripts are).

So, probably the best way to create this bar is to use sprites. Sprites, different from simply drawing sqares in your screen, enable you more detailed textures and can be simply changed by changing the .JPG or .PNG file. To do a bar you’d need 2 sprites, one for the empty bar (e.g: grey square) and one for the full part of the bar (e.g: purple square).

This is the first code you’ll need:

using UnityEngine;
using System.Collections;

public class IMGUIThingy : MonoBehaviour {

	Texture2D emptyBar;
	Texture2D fullBar;

	Vector2 position; //This is where you want the top-left corner to be in your screen.

	[Range (0,100)] //This is the range between your fullPorcentage's value can be in (min = 0 and max = 10).
	public float fullPorcentage;

	float barWidth;
	float barHeight;

	public void Awake () {
		emptyBar = Resources.Load<Texture2D> ("Empty"); //This will find your texture using the file name.
		fullBar = Resources.Load<Texture2D> ("Full");
	}

	//All this variaables are here because the Screen's size might change.
	public void Update () {
		position.x = Screen.width * 0.15f;
		position.y = Screen.height * 0.75f;
		//This is at the bottom of the screen

		barWidth = Screen.width * 0.7f;
		barHeight = Screen.height * 0.15f;
	}

	//Here you will put all the GUI methods you need.
	public void OnGUI () {
		GUI.DrawTexture (new Rect (position, new Vector2 (barWidth, barHeight)), emptyBar);
		GUI.DrawTexture (new Rect (position, new Vector2 (barWidth / 100 * fullPorcentage, barHeight)), fullBar);
	}
}

When you finish this you’ll see that your camera has one variable where you can select fullPorcentage’s value, and as you change it the purpule part of the bar will change. If you want the bar to decrease and just to increase when something happens you just add these details:

	public float decreaseAmountPerSecond;
	public float increaseAmountWhen3InRow;

	public void Update () {
		//Constantly decrease
		fullPorcentage -= decreaseAmountPerSecond * Time.deltaTime;
	}

	//Call this method when 3-in-Row happens
	public void ThreeInRow () {
		fullPorcentage += increaseAmountWhen3InRow;
	}

Hope this helps you a lot! :smiley: