- Home /
Question by
Skyfall106_Gaming · May 23, 2017 at 06:22 AM ·
c#unity 5
Money System Scripting Problem
Hey! So I am trying to implement a Money System where the buildings cost money to place and houses generate income and towers take income but there is a error for trying to make houses cost money. Here is the problem
ScoreManager sm = GameObject.FindObjectOfType<ScoreManager> ();
if(sm.money < bm.SelectedBuilding.GetComponent<house>().cost) {
return;
}
Im not sure what the problem is and if somebody could help it would be great (Everything after and including GetComponent is red)
Here is my whole script.
using UnityEngine;
using UnityEngine.UI;
public class GUIScript : MonoBehaviour {
BuildManager bm;
Button house, tower, path, field;
bool buildopen = false;
// Use this for initialization
void Start () {
bm = GameObject.Find("BuildManager").GetComponent<BuildManager>();
Canvas cv = GameObject.Find("Canvas").GetComponent<Canvas>();
house = cv.transform.FindChild("HouseButton").gameObject.GetComponent<Button>();
tower = cv.transform.FindChild("TowerButton").gameObject.GetComponent<Button>();
path = cv.transform.FindChild("PathButton").gameObject.GetComponent<Button>();
field = cv.transform.FindChild("FieldButton").gameObject.GetComponent<Button>();
}
public void ActiveteBuilding(Button pressedBtn)
{
if (pressedBtn.name == "BuildButton")
{
if (buildopen)
{
//bm.DeactivateBuildingmode();
house.gameObject.SetActive(false);
tower.gameObject.SetActive(false);
path.gameObject.SetActive(false);
field.gameObject.SetActive(false);
pressedBtn.image.color = Color.white;
buildopen = false;
}
else
{
//bm.ActivateBuildingmode();
house.gameObject.SetActive(true);
tower.gameObject.SetActive(true);
path.gameObject.SetActive(true);
field.gameObject.SetActive(true);
pressedBtn.image.color = new Color(255, 0, 255);
buildopen = true;
}
}
else
{
switch (pressedBtn.name)
{
case "HouseButton":
bm.SelectBuilding(0);
break;
case "TowerButton":
bm.SelectBuilding(1);
break;
case "PathButton":
bm.SelectBuilding(2);
break;
case "FieldButton":
bm.SelectBuilding(3);
break;
}
pressedBtn.image.color = new Color(155, 120, 255);
bm.ActivateBuildingmode();
}
}
void Update()
{
if (buildopen)
{
if (!bm.isBuildingEnabled)
{
if (house.image.color != Color.white)
house.image.color = Color.white;
if (tower.image.color != Color.white)
tower.image.color = Color.white;
if (path.image.color != Color.white)
path.image.color = Color.white;
if (field.image.color != Color.white)
field.image.color = Color.white;
}
}
ScoreManager sm = GameObject.FindObjectOfType<ScoreManager> ();
if(sm.money < bm.SelectedBuilding.GetComponent<house>().cost) {
return;
}
}
}
Comment
I think you just need to declare your get component to a variable first
Var h = bm.SelectedBuilding.GetComponent<house>();
If (sm.money < h.cost)
{
//do stuff
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Object from list in scriptableobject in list 0 Answers
How to define a slider on script file? 1 Answer
How to make buttons have sound when it is highlighted and clicked? 0 Answers