Game Logic - How to Check if player has enough Coins before Instanstiating a House?

Okay - Currently, a Player runs around, and collects Coins, timber, etc... Its populated on a GUI (sort of like an Inventory system) It places each object (which are stack able) into the Inventory box.

What i want to do, is when the player goes to another GUI area, to say - Build House, or Plant Tree, - The Game checks to see if it has X available Coins. If not, it flashes a. YOU CANT DO THAT. if it does, then it allows me to Spawn a Prefab to the location of the players choice.

Question: How do i make it check to see whats available in the inventory?

Any scripts would be fantastic. something i can dissect and pick apart, to learn the inner workings, of course, i am completely happy with a "Here is a 200 line Script that does just that! written for you! for free! drop it on X object" :)

I'm not going to write a functional code for you. I will write a code missing your data, so that all's you have to do is plug it in. This way, you have to read through it, but have a guide.

  1. Make sure your inventory system is setup in such a way that this won't be too hard. Your inventory system should include variables for the current amount of all your resources. If this isn't setup, determine how you want to add to your counter, maybe `OnCollisionEnter () { Resource++; }`

  2. Set up your box to grab the inventory script then read the variable whenever it is needed. Not sure what GUI you are using. It will work very similarly no matter which.


var inventoryScript : #NameOfInventoryScriptHere#;

function Start () {

     FindObjectOfType(#NameOfInventoryScriptHere#); //needs cast in C# unless generic
     //cache a link to the inventory.

}

function OnGUI () {
     //You could use GUI layout also
     if(GUI.Button(#buttonArguments#)) {
     //create a button to respond if the user has enough of a certain resource.
     //this button might check gold and another might check food or something.

         if(inventoryScript.resource >= certainValue) {
         //see if we have enough of that resource

              inventoryScript.resource -= certainValue;
              CreateObjectAssociatedWithButton();
         }
     }
}

function CreateObjectAssociatedWithButton () {
}