Skip to content

Instantly share code, notes, and snippets.

@AkshatGiri
Last active June 22, 2016 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkshatGiri/5229fd7e6a17d663a3de9f7cf353b72c to your computer and use it in GitHub Desktop.
Save AkshatGiri/5229fd7e6a17d663a3de9f7cf353b72c to your computer and use it in GitHub Desktop.
Changing brick color
using UnityEngine;
using System.Collections;
public class brick : MonoBehaviour {
public int maxHits;
private int timesHit;
private LevelManager levelManager;
int notTimesHit;
//I made notTimesHit So later i can use it to change color
// Use this for initialization
void Start () {
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D col)
{
timesHit++;
if (timesHit >= maxHits)
{
Destroy(gameObject);
} else
{
/*Here i subtracted maxhits - timesHit because then i would get a number which i could use later to determine which brick it is
and what color should it be changed to. */
notTimesHit = maxHits - timesHit;
changeColor();
}
}
void changeColor()
{
if (notTimesHit == 2)
{
this.GetComponent<SpriteRenderer>().color = new Color(0, 1, 0, 1);
// (0, 1, 0,1) is code for blue
}
if (notTimesHit == 1)
this.GetComponent<SpriteRenderer>().color = new Color(251f, 255f, 0f, 255f);
//(251f, 255f, 0f, 255f) is code for yellow
}
void SimulateWin()
{
levelManager.LoadNextLevel();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment