Last active
June 22, 2016 05:33
-
-
Save AkshatGiri/5229fd7e6a17d663a3de9f7cf353b72c to your computer and use it in GitHub Desktop.
Changing brick color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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