- Home /
Charater selection with coins.
Hi! I have a plataform game mostly done, and i want to add coins in to the game and a charater selection so you can change your character expending some of your collected coins. How can i do this? I have searched for tutorials but i didnt found nothing. Please help!
Answer by LukeWaffel · Aug 29, 2017 at 07:29 AM
Hi!
I'm not sure wether or not you're looking for actual code examples or just a general way of thinking on how to implement the requested features, but for this post, I'll assume the latter.
First of all, you'd need to create some kind of coin prefab in your game. It should have a model (Or Sprite if your game is in 2D), appropriate colliders and a script. The script should check for trigger collisions with the player (A simple OnTriggerEnter with a tag or GameObject comparison should do the trick). Upon collision with the player, increase the amount of coins the player has collected during his current run. The amount of collected coins can be store in any script (Presumably the player script or a seperate score script).
When the player dies, save the amount of collected coins to either a file or the PlayerPrefs system (The latter is the easiest). You can use PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins") + collectedCoinsVariable); All this does is increase the current saved Coins value by the amount of collected coins. Make sure to call PlayerPrefs.Save() after setting the int, otherwise the values won't get saved.
In your main menu, you can now display the total amount of coins by displaying the value of PlayerPrefs.GetInt("Coins").
For the character selection, you'll need to create a new script to hold information about each character. You'd start by creating a new script and making sure it doesn't inherit MonoBehaviour (You can easily do this by removing the : MonoBehaviour at the top of the script). You can now create variables for the name, description, price, model and what ever you want. Make sure to add a variable for keeping track wether or not the player owns the current character
Create a second script, which will act as the character store. That script will have a List of the just created character information script. You can now add as many characters as you want to the list. To purchase a character, create a function to check if the user has enough money, remove the required amount of coins if he has, and set the variable for keeping track of wether or not the user owns the character to true. For every character that the player owns, you can change the buy button to a select button. You can create a variable of the custom character info type to store the current selected character.
Now, in game, you can simply switch out the player model with the player model of the currently selected character.
I hope this somewhat helped!
Your answer
![](https://koobas.hobune.stream/wayback/20220612133748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make a Interactive Computer Screen in Unity 5.5? 1 Answer
Access function in another script cause Object reference error 1 Answer
Unlocking & "Equipping" Colors onto Sprites with c# 2 Answers
How can I make my navmesh agent stop in between travel to destination points? 0 Answers