So i have a array that will continuously add buildings every time the player places a building. So how would I set it up to take money from the same script and decrease it by a certain amount every time a building is placed. Please help me
here is my script for reference c#
using UnityEngine;
using System.Collections;
public class Game : MonoBehaviour {
public int defaultMoney = 100;
public int minMoney = 0;
public int maxMoney = 20000;
public string moneySign = "$";
//*************************
public Object[] buildings = {};
private int buildingCount;
public Object[] buildingsuz = {};
private int buildingCountuz;
public Object[] buildingsuo = {};
private int buildingCountuo;
//*************************
public GUIContent content;
public Texture2D building1;
public Texture2D building2;
public Texture2D skyScraper;
public Texture2D gvcc;
public Texture2D courtHouse;
public Texture2D powerStation;
public Texture2D road;
//*************************
public bool isNormalSpeed;
// Use this for initialization
void Start () {
building1 = Resources.Load("Building1") as Texture2D;
building2 = Resources.Load("Building2") as Texture2D;
skyScraper = Resources.Load("SkyScraper") as Texture2D;
gvcc = Resources.Load("gvcc") as Texture2D;
courtHouse = Resources.Load("CourtHouse") as Texture2D;
powerStation = Resources.Load("PowerStation") as Texture2D;
road = Resources.Load ("Road") as Texture2D;
InvokeRepeating("CountUp", 5, 5);
}
void CountUp(){
foreach(Object item in buildingsuz)
{
defaultMoney += 10;
}
foreach(Object item in buildingsuo)
{
defaultMoney += 30;
}
}
// Update is called once per frame
void Update () {
if (maxMoney <= minMoney) {
Application.Quit ();
}
buildings = GameObject.FindGameObjectsWithTag ("Building");
buildingCount = buildings.Length;
buildingsuz= GameObject.FindGameObjectsWithTag ("Buildinguz");
buildingCountuz = buildingsuz.Length;
buildingsuo= GameObject.FindGameObjectsWithTag ("Buildinguo");
buildingCountuo = buildingsuo.Length;
if (defaultMoney >= maxMoney) {
defaultMoney = maxMoney;
}
else if (defaultMoney <= minMoney) {
defaultMoney = maxMoney;
}
Debug.Log ("What is normal Speed at: " + isNormalSpeed);
}
void OnGUI(){
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 5 / 100;
style.normal.textColor = new Color (1.0f, 1.0f, 1.0f, 1.0f);
string text = string.Format("Money:" + moneySign + defaultMoney);
GUI.Label(rect, text, style);
GUI.TextArea (new Rect (740, 10, 100, 30), "Buildings: " + buildingCount);
if (Input.GetKey (KeyCode.B)) {
GUI.Box (new Rect (10, 50, 200, 200), "Buildings");
GUI.skin.button.normal.background = building1;
GUI.skin.button.hover.background = building1;
GUI.skin.button.active.background = building1;
if (GUI.Button (new Rect (20, 70, 50, 50), "")) {
}
GUI.skin.button.normal.background = building2;
GUI.skin.button.hover.background = building2;
GUI.skin.button.active.background = building2;
if (GUI.Button (new Rect (80, 70, 50, 50), "")) {
}
GUI.skin.button.normal.background = skyScraper;
GUI.skin.button.hover.background = skyScraper;
GUI.skin.button.active.background = skyScraper;
if (GUI.Button (new Rect (140, 70, 50, 50), "")) {
}
GUI.skin.button.normal.background = gvcc;
GUI.skin.button.hover.background = gvcc;
GUI.skin.button.active.background = gvcc;
if (GUI.Button (new Rect (20, 140, 50, 50), "")) {
}
GUI.skin.button.normal.background = courtHouse;
GUI.skin.button.hover.background = courtHouse;
GUI.skin.button.active.background = courtHouse;
if (GUI.Button (new Rect (80, 140, 50, 50), "")) {
}
GUI.skin.button.normal.background = powerStation;
GUI.skin.button.hover.background = powerStation;
GUI.skin.button.active.background = powerStation;
if (GUI.Button (new Rect (140, 140, 50, 50), "")) {
}
}
if (Input.GetKey (KeyCode.M)) {
GUI.Box (new Rect (635, 50, 200, 200), "miscellaneous");
GUI.skin.button.normal.background = road;
GUI.skin.button.hover.background = road;
GUI.skin.button.active.background = road;
if (GUI.Button (new Rect (645, 70, 50, 50), "")) {
}
}
if (Input.GetKey (KeyCode.E)) {
GUI.Box (new Rect (400, 40, 100, 100), "Road Options");
if (GUI.Toggle (new Rect (410, 60, 50, 50), false, "Left")) {
}
if (GUI.Toggle (new Rect (410, 90, 50, 50), false, "Right")) {
}
if (GUI.Toggle (new Rect (410, 120, 90, 90), false, "Intersection")) {
}
}
if (Input.GetKey (KeyCode.U)) {
GUI.Button (new Rect (400, 40, 100, 50), "Buildings with Upgrade Zero: " + buildingCountuz);
GUI.TextArea (new Rect (400, 80, 100, 50), "Buildings with Upgrade One: " + buildingCountuo);
}
}
void FixedUpdate(){
if (Input.GetKeyDown (KeyCode.P)) {
if(isNormalSpeed == true){
Time.timeScale = 2.0f;
isNormalSpeed = false;
}
else if(isNormalSpeed == false){
Time.timeScale = 1.0f;
isNormalSpeed = true;
}
}
}
}