i have few player in my game , i need after getting sufficient amount of score coins , player of game can unlock other locked players, any help ?
create a script that stores an Int value called coins. Then when ever they player does something good, add to the coins int.
you need a menu or store system to display a available things to buy. If the player chooses to buy it, remove from the coins int
it is hard to understand for me your explanation . i have a game manager script in which i have score int variable available. whenever player in game gets coins , this score variables counts up . i have some ui buttons which presents my other players to get bought . this is the question , how to check if player has enough score to buy one of those other players ? can you please show your idea with sone ground codes . thanks .
if(playerCoin >= price of item)
{
allow purchasse
}
to which game object should i attach this code?
You will need to make different scripts. I have never did this, but i am going, so i think what you need to do is similar.
The player will get points eventually, right? You must have a script that will add points to the player each time he does something. What you need to do, is make a script that will have an int/float (you decide) in which it will store the player’s points. Then you need to make another script that will hold the thing you want to buy, like another character. In this script, you will need to specify the amount of points the player will need to buy determined thing, and check if he have enough points, then he can buy it.
This is something that is pretty simple, but also extremely implementation based.
If you cannot write this yourself then you need to familiarize yourself with C# / JavaScript. I would suggest reading their documentations.
You might find a lot of good information here: Create a Character Select System with Scriptable Objects - Unity Learn This set of videos goes through a character/ability select system, which probably wouldn’t take much tweaking to gate behind a points/coin value.
Thanks all of you friends but what i want to do is like bryangs told .i have playerA which has a script attached to in gameplay scene .also in shop scene i have playerB and playerC and playerD . at first only playerA is unlocked and is playable .but i need when i have 1000 score obtained i buy playerB . in shop scene i have a script attached to a game object for saving and showing current score with a ui text . in this scene i have a list of game objects which present playerB ::: playerD , Here i need to address current obtained amount of score ,that is it possible to buy new player or not .
if somebody with codes show that , it is all useful .
thanks .
What script is keeping track of if the character has been purchased or not?
I might do this with a bool attached to each character. pubic bool isPurchased;
Then you’re store script has to read this bool. If bool is false then allow to purchase for 1000 coins. If bool is true then allow character to be used.
The main problem with your post is we don’t have all he info to give you direct answers.
I can tell you exactly how, as I already have done this exact same thing in my game (C#):
Step 1: Currency
We want the money being universal, the same in all scenes, the same for everywhere, if you change it in Scene A, it will be changed in all other scenes too. How to do this? pretty simple.
1.- Create a new C# script and name it GlobalValues, cause we will use it later, then, you will find:
public class GlobalValues : MonoBehaviour
make it a static:
public static class GlobalValues : MonoBehaviour
As it is static, you dont need to create a gameObject in every scene to carry it. It runs alone, (EDIT: so you wont need : MonoBehaviour).
Add a public static float named “money”:
//public class SpriteHolder : NetworkBehaviour {
//public static float money;
//} Sorry I missed this, IGNORE THIS PART IN //s, Use the one below
//Its just that I made a small mistake and... well It was 3 days ago, dont remember
//what I did but, well Ignore that above, use the one below
public static class GlobalValues {
public static float money;
}
There you go, you got an universal money value.
Step 2: The Character Shop
I dont know how exactly your game is, but I am pretty sure the shop is in another scene, Id recommend to do a 2D shop, so you can use buttons from UI and it will look good, this is a bit weird in 3D, well thats your choice. well, you should open GlobalValues and add one more public static int:
public static int character
additionally, one public static bool for every unlockable character:
public static bool character2Unlocked;
public static bool character3Unlocked;
public static bool character4Unlocked;
Now, here is an image of how you could do the interface (2D):
This is how it would look, I dont know what yout game is about so i made one version, as you can see there is an exit button, the characters, and in case you have more characters than those who fit the screen you add next page and prevous page buttons.
Step 3: The buy/select mechanics
With your shop built, then we proceed to the next step, you add a button below each character.
Then you make the following C# script, named BuyScript:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; //Lets you use UI stuff
public class BuyScript : MonoBehaviour {
public int myChar; //Number assigned manually for the character number.
private float price; //The price
public Text text; //Where the price is displayed, that will change
//to select and selected when already unlocked
void Start() {
price = GetComponent<BuyScript2>().price; //Will be marked as an error now as
//There is no BuyScript 2 YET
void Update()
{
if (myChar == 1) //If its the first character
{
if (GlobalValues.character == 1) //If character is 1
{
text.text = "selected"; //Changes the text to "Selected", as
//It is character 1 and GlobalValues.character
//is also 1.
}
else //If its not character 1
{
text.text = "select" //Changes the text to select
}
} //Note that character one is the one you start with, so has no buy stuff.}
if (myChar == 2) //If its the second character
{
if (GlobalValues.character2Unlocked) //If it has been unlocked
{
if (GlobalValues.character == 2) //If character is 2
{
text.text = "selected"; //Changes the text to "Selected", as
//It is character 2 and GlobalValues.character
//is also 2.
}
else //If GlobalValues.character is not 2
{
text.text = "select" //Changes the text to select, cause it isnt selected
}
}
else //If it hasnt been unlocked
{
text.text = "$" + price; //Sets the text to display the price.
}
}
//The same code of myChar 2 for every single other character, but changint
//the number 2 to the number of character
}
}
This script only checks:
- If you have bought the character
- If you have selected the character
Now lets make the one that buys/selects the character, name it BuyScript2:
using UnityEngine;
using System.Collections;
public class BuyScript2 : MonoBehaviour {
private int myChar; //This time there is no need to assign this one
public float price; //Price, manually set in the inspector
void Awake() //This is run even if the script is off
{
myChar = GetComponent<BuyScript>().myChar;
//Sets this myChar value to the one in BuyScript,
//So you dont have to set it twice.
}
void Update()
{
if (myChar == 1) { GlobalValues.character = 1; } //Selects Char 1 if it
//this is char 1 button.
if (myChar == 2) //If This is Character 2 button
{
if (GlobalValues.character2Unlocked) //If you have unlocked character 2
{
GlobalValues.character = 2; //Selects character 2
}
else //If you havent unlocked character two
{
if (GlobalValues.money >= price) //If you have greater or equal money than the price
{
GlobalValues.money = GlobalValues.money - price; //Substracts the price from
//your money
}
//If you dont have greater or equal money than the price nothing happens.
}
}
//And the same for every other character, changing the instances of 2.
GetComponent<BuyScript2>().enabled = false; Turns the script off AT THE END
}
}
Step 3: Buy/Select part 2
We have the scripts, time to config them in the inspector.
Nothing better than an image to explain it simple and fast
You can see that each button has BuyScript and BuyScript 2, with their respective values, also have their own text component, attached to the BuyScript and the most important: BuyScript2 is not enabled.
Here is an image of the Button Component Stuff:
Remember that this button component is in BuyButton1, BuyButton2’s enables its own BuyScript2.
At this moment, it is working, this setup lets you buy new characters and select the ones you already unlicked, but they keep showing the locked character image :p.
Now lets make them “Unlock” in the image too:
Step 4: Unlock Image
The following script goes attached to the characters images not their Buy Button.
using UnityEngine;
using System.Collections;
using UnityEngine.UI; //This one needs UI
public class unlockedScript : MonoBehaviour {
public int myChar;
public Sprite char2;
public Sprite char3;
public Sprite char4;
private bool changed;
void Update()
{
if (changed == false) //If it hasnt changed the sprite
{
if (GlobalValues.character2Unlocked && myChar == 2) { //If you have unlocked Char2 and this is Char2 Image
GetComponent<Image>().sprite = char2; //Changes to unlocked Char2 Image
changed = true;
}
if (GlobalValues.character3Unlocked && myChar == 3) {
GetComponent<Image>().sprite = char3; //The same but with Char 3
changed = true;
}
if (GlobalValues.character4Unlocked && myChar ==4) {
GetComponent<Image>().sprite = char4; //The same but with Char 4
changed = true;
}
//And so on for every character you have
}
}
}
This has nothing to do about the problem, but I really wanted to draw the unlocked character versions:
Image of all Unlocked
Last Step: Color/Image of the Buttons
Well, when you already bought the character, you dont really want to have a red button displaying “Selected” or “Select”, to do this we go back to “BuyScript”, and add the following:
At the beginning:
public Image image;
*This one is the image of the button, assigned in the inspector.
In the code for every character:
if (myChar == 2) //If its the second character
{
if (GlobalValues.character2Unlocked) //We already have this part of the code
{
if (GlobalValues.character == 2)
{
text.text = "selected";
image.color = Color.green; //We add this one
}
else //If its not character 2
{
text.text = "select"
image.color = Color.cyan; //We add this one
}
}
else //If it hasnt been unlocked
{
text.text = "$" + price; //Sets the text to display the price.
image.color = Color.red; //Not really necesary, but there is nothing to loose
}
}
If you instead want to change to a different sprite instead of a color when bought, tell me to add a Sprite version, as I dont think you will actually want to change sprite, I dont see the need to give an example.
Hope this helps ![]()
but in picture it is enabled,why?
There are two BuyScripts, one always enabled and the otherone only enables for a single frame, the one enabled is BuyScript, only BuyScript, the one not enabled is BuyScript2, and is below. The picture shows correctly.
The reason for this, is that the one always enabled, changes the text, and it doesnt matter to repeat every frame, while the one that only enables when the button is clicked is the one that buys the character, if it was enabled always, then as you enter the shop, if you have enough money every character you can buy with your current money will buy automatically, without asking, and between the ones you already have will be selected every frame.
If you want a sign to display your money, the line is simple: (You need at the start of the script Using.UnityEngine.UI; and a Text Component attached to the gameObject).
GetComponent().text = "Your Money: " + GlobalValues.money;
Note that space after the : cause if you erase it, the money will apear as “Your Money:100”, else it will appear as “Your Money: 100”, I think is nicer with the space, but do it as you like it.
when i run my game , when i click on character 1 , it says selected . when i click on any other player to buy , simply no things goes on. also i have already a ui text to show current obtained score. how can i get obtained score from gameplay scene and show that score with this ui text ? and after that to compare if this score is enough to buy locked character?thanks .
Make a new code, attach it to the text that shows the current score and use the inspector to set the public text:
Using.UnityEngine;
Using.System.Collections;
Using.System.Collections.UI;
public class ShowScoreText : MonoBehaviour {
public Text scoreText;
void Update() {
scoreText.text = "Your score is: " + GlobalValues.money;
}
}
Also to change your score easily, you can open GlobalValues and do the following:
public static float money = 1000;
The 1000 value is whatever you like, enough to buy the character you want to test funcionalitiy for
i did like you said,now ui text shows current money which is 1000 . but when i click on one player to buy and unlock it , nothing again goes on ! strange !!! what have i done wrong , you think?
i attached BuyScript and BuyScript2 to every button that i have for buying different characters.
i dargged button1 from Hierarchy to On Click slot and then i chose BuyScript2 and then bool enabled . after that i checked to make it active . i repeated this step for all of buttons.
Hmph, Try to send a pic of the components and the script BuyScript2, if we cant solve the problem this way, I will make a video and send it to you.
Super! thanks.
Ok, if that does not work, Im starting the video xd