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

[LIST=1]
[*]          ScoreManager sm = GameObject.FindObjectOfType<ScoreManager> ();
[*]          if(sm.money < bm.SelectedBuilding.GetComponent<house>().cost) {
[*]              return;
[*]          }


[/LIST]

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.

[LIST=1]
[*]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;
[*]         }
[*]     }
[*]}
[/LIST]

Sounds like a bug with your ide. Does unity give you any errors?

could do with seeing the BuildManager scrip, try

bm.SelectedBuilding().GetComponent<house>().cost

I ahev 2 errors:

Assets/Placement Kit/Example Project Files/Scripts/GUIScript.cs(93,37): error CS1061: Type int' does not contain a definition for GetComponent’ and no extension method GetComponent' of type int’ could be found. Are you missing an assembly reference?

and

[Collab] UnityCloud::PersistentFaraday::Failure: Request failed with response code 401 from http://localhost:5001/v1/core/api/users/me: the server responded with status 401

Hope that helps (I can send build manager to)

There is an error on in this script - GUIScript.cs. Just double click the message and it should take you straight to it or post the code here.

But im not sure what it is because getComponent worked before in the script

It tells you the problem, you are trying to use GetComponent on an int. Just post the code.

         if(sm.money < bm.SelectedBuilding.GetComponent<house>().cost) {

I can only assume that SelectedBuilding is an int and not a gameobject.

it is a int. How can i fix that so this works?

You need to get the house component from whichever gameobject has it. I have no idea how you game is laid out.

its attached to the BuildManager object

Just bumping because I need a answer ASAP

glancing over the most recent posts, I’d say you’re looking for:

 bm.GetComponent<house>().cost;

Half a guess in the dark there. :wink: