Skip to content

Instantly share code, notes, and snippets.

@kihira
Last active October 19, 2015 08:38
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 kihira/02eb64e1fe2c4680355b to your computer and use it in GitHub Desktop.
Save kihira/02eb64e1fe2c4680355b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
/// <summary>
/// Attach this script to any object that modifies the score on its death
/// </summary>
public class Score : MonoBehaviour {
public int score = 100;
void OnDestroy()
{
GameObject.Find("Score").GetComponent<ScoreManager>().AddScore(score);
}
}
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
private int currScore;
private List<int> scores;
void Start () {
scores = new List<int>();
}
void Update () {
if (scores.Count <= 0) return;
scores[0] -= 2;
currScore += 2;
if (scores[0] <= 0)
{
scores.RemoveAt(0);
}
// Update text display for the score
var textComponent = gameObject.GetComponent<Text>();
textComponent.text = currScore.ToString().PadLeft(8, '0');
}
public void AddScore(int score)
{
scores.Add(score);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment