- Home /
Shop with Money
I have a 50% done game, but the only thing I need is a shop feature in my game. My Question is how to make a shop in my game represented as a GUI Background and a few Buttons
For example:
The player comes to a Game Object in the scene called Shop(Prefab). So there are 10 coins in the my scene, but then later I will add more coins on the way. And when the pop up message appears, the player can click the CTRL or F button to access the Shop. After that a GUI Texture appears as a Background and a Few 3D Texts saying "Buy", "Sell" and "Quit".
Felipe
How can I make a Shop in the game using the coins in my level
Answer by Wolfie · Feb 04, 2012 at 08:49 PM
There are so many ways to do this that it's almost like asking "How do I make a game?"
To tell the truth the best thing you could do is to look up some tutorials, read books on scripting and to have a good rummage through the Unity Scripting Reference. The best way to work out solutions to problems like this is to start out knowing what tools are at your disposal. Each solution will have pros and cons, and because we can't tell you how you want your game it's really all down to you to decide what's best.
Anyway, one very simple way to do it is to have a script to keep track of your money. In this you'd have an Int variable which counts how many coins your player has collected.
var coinsCollected : int = 0;
When your player collides with something you check to see if it's a coin (using the object's name, or its tag) then, if it's a coin you destroy it and add one to your variable. If you use the object's name, make sure that's the same as what you tell the script to look for, or similarly if you use a tag, make sure that your coin prefab has the same tag that your script uses. I guess that's kinda obvious really.
function OnCollisionEnter(collision : Collision){
if (collision.gameObject.name == "Coin"){ // OR // if (collision.gameObject.tag == "Coin"){
Destroy(collision.gameObject);
coinsCollected ++;
}
}
Next go to your shop, and have a prompt appear when you are within a certain distance. So, in an Update function use another 'if' statement to check how far you are from it. Alternatively you can use a spherical trigger collider. That one's basically the same as the collecting coins example, but uses OnTriggerEnter instead of OnCollisionEnter so I won't go through that. Anyway, for method one, to check how far apart two things are you have to compare their coordinates, which are vectors. Conveniently enough all you need to do is subtract one from the other to tell how far apart they are. Stuck together it would look something like;
var guiShopIsOpen : boolean = false; // This is to tell OnGUI wether or not it should show the shop interface or not
function Update(){
var distanceToShop = player.position - shop.position; // Note that player and shop may be different things, for example, if this script is attached to the player, then it would just be transform.position instead.
if (distanceToShop < 5){ // See if we're closer than 5 meters from the shop
if (GetButtonDown("Enter Shop")){ // Note that your key's name probably won't be Enter Shop, just make sure it's the same as a key that's been defined in Edit -> Project Settings -> Input. You said you'd use Ctrl or F, so just set them to be the ones you use.
guiShopIsOpen = true; // You can now shop away!
}
}
}
Finally, to have an actual shop interface you could use OnGui. I won't go through all this as there are lots of possible ways you'd want to go about it, however the documentation is here and it's pretty comprehensive, with a good bunch of examples.
Using that, slap together an interface that fills your heart with gladness, and you're away. Just put something in it that uses another 'if' statement to check if you've got enough money to afford an item, and if you have then subtract the item's cost from coinsCollected. Something like;
function OnGUI () {
if (guiShopIsOpen == true) // Should we bother to show any of this? ie. Is the shop interface even open?
if (GUI.Button (Rect (10,10,200,30), "Buy Stuff!") && coinsCollected >= 8) { // Assuming the object costs 8 coins.
coinsCollected -= 8; // Take away some of the player's coins.
// Give item to the player scriptness goes here
}
if (GUI.Button (Rect (10,50,200,30), "Exit Shop")){ // Create an Exit Shop button and see if it's being clicked on, if it is then leave the shop.
guiShopIsOpen = false;
}
}
}
Well there we go, I hope I get an 'Accuracy By Volume' award for this one. Best of luck and don't forget that there are probably hundreds of other ways to do this same stuff. Scripting isn't about doing it right, it's about doing it better.
Also, I haven't tested my script at all, so it may well not actually work. It's just a guideline, so no copy-pasting 'cos you won't learn anything that way. :D
Your answer
Follow this Question
Related Questions
Can I assign controls to GUITexture button? 1 Answer
GUI grid of buttons issue. 1 Answer
Strange GUITexture Bug?? 0 Answers