- Home /
How can I use 'buy character button'?
I made a character selection system and ı added 'buy button' for characters. I want that the player can select a character If player buys that character but he cannot continue the game without buying that character. How can I make this buying process? I added 'buy button' for some characters. When the player presses this button, the button is deleted and his money decreases. But I don't want the player to choose that character without pressing the buy button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Characters : MonoBehaviour
{
public GameObject[] characters;
public int selectedCharacter;
public int gold=450;
public Text goldText;
public GameObject[] buyButtons;
public bool bought;
void Start()
{
goldText.text="Gold:" + gold.ToString();
selectedCharacter=PlayerPrefs.GetInt("selectedCharacter");
characters=new GameObject[transform.childCount];
for (int i = 0; i < characters.Length; i++)
{
characters[i]=transform.GetChild(i).gameObject;
}
foreach (GameObject go in characters)
{
go.SetActive(false);
}
if (characters[selectedCharacter])
{
characters[selectedCharacter].SetActive(true);
}
//First four characters are bought!
if (selectedCharacter>4)
{
bought=false;
}
else
{
bought=true;
}
}
public void PreviousCharacter()
{
characters[selectedCharacter].SetActive(false);
selectedCharacter--;
if (selectedCharacter < 0)
{
selectedCharacter=characters.Length-1;
}
characters[selectedCharacter].SetActive(true);
}
public void NextCharacter()
{
characters[selectedCharacter].SetActive(false);
selectedCharacter++;
if (selectedCharacter == characters.Length)
{
selectedCharacter=0;
}
characters[selectedCharacter].SetActive(true);
}
public void StartGame()
{
if (bought)
{
PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
SceneManager.LoadScene(1,LoadSceneMode.Single);
}
else
{
Debug.Log("cannot start");
}
// PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
// SceneManager.LoadScene(1,LoadSceneMode.Single);
}
public void BuyButton()
{
if (gold >= 100)
{
buyButtons[0].SetActive(false);
gold-=100;
goldText.text="Gold:" + gold.ToString();
bought=true;
}
}
public void BuyButton1()
{
if (gold >= 100)
{
buyButtons[1].SetActive(false);
gold-=100;
goldText.text="Gold:" + gold.ToString();
bought=true;
}
}
public void BuyButton2()
{
if (gold >= 100)
{
buyButtons[2].SetActive(false);
gold-=100;
goldText.text="Gold:" + gold.ToString();
bought=true;
}
}
public void BuyButton3()
{
if (gold >= 100)
{
buyButtons[3].SetActive(false);
gold-=100;
goldText.text="Gold:" + gold.ToString();
bought=true;
}
}
public void BuyButton4()
{
if (gold >= 100)
{
buyButtons[4].SetActive(false);
gold-=100;
goldText.text="Gold:" + gold.ToString();
bought=true;
}
}
}
Answer by armandas2005 · Aug 26, 2021 at 05:10 PM
Hi, so i made this system in 3d. I used scriptableObjects for that. first you have to put the characters script on a empty object. If you done that you have to add characters in character list, to do that you have to rightclick in your assets folder and go to c(create > character). if you done that u have to declare a character object in there and the gold amount(how much it cost) (IMPORTANT: the object must have the dontdestroyonload script!!!). After u done that u have to add this character object to the characters list. If you done all that u have to make some buttons (Rightclick in scene > UI > Button). In the button there is a "component" called "On Click()". in the bottom right corner of this component there is a "+", you have to click on that. In the object " section" u have to add your empty Gameobject with the characters script on it. If you done that u have to click on Function and go to the character section. In there u can select functions of the script, like "next" or "buy". that should be it.
Add this script on a empty GameObject using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class Characters : MonoBehaviour
{
public List<Character> characters;
public int selectedCharacter;
public int charactersInList;
public int gold = 450;
public Text goldText;
public GameObject previewObject;
public bool canStart;
void Start()
{
previewObject = Instantiate(characters[selectedCharacter].characterObject, transform.position, transform.rotation);
goldText.text = "Gold:" + gold.ToString();
selectedCharacter = PlayerPrefs.GetInt("selectedCharacter");
Refresh();
}
public void Refresh()
{
charactersInList = 0;
for (int i = 0; i < characters.Count; i++)
{
charactersInList++;
}
charactersInList--;
}
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
Next();
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
Back();
}
}
private void SelectSkin()
{
if(previewObject != null)
{
Destroy(previewObject);
previewObject = Instantiate(characters[selectedCharacter].characterObject, transform.position, transform.rotation);
}
Debug.Log("m");
}
public void Next()
{
if (selectedCharacter < charactersInList)
{
selectedCharacter++;
SelectSkin();
}
}
public void Back()
{
if (selectedCharacter > 0)
{
selectedCharacter--;
SelectSkin();
}
}
public void StartGame()
{
if (canStart)
{
PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
SceneManager.LoadScene(1, LoadSceneMode.Single);
}
else
{
Debug.Log("you have to pay First!");
}
}
void Buy()
{
if(gold >= characters[selectedCharacter].Gold)
{
gold -= characters[selectedCharacter].Gold;
canStart = true;
StartGame();
}
}
}
Dont do this script on any object, it just have to be in your asset folder(to create characters click (create > characters)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class Character : ScriptableObject
{
public int Gold;
public GameObject characterObject;
}
To every character visual object u have to add this script, so the object dont disappear after loading into a new scene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DontDestroy : MonoBehaviour
{
public void Awake()
{
DontDestroyOnLoad(this.gameObject); //Other stuff
}
}
I hope i could help if you have any questions or u run into some issues with these scripts, tell me, i will give my best to help you :D
Thank you very much for answering. In my codes I have made some changes and now the player cannot continue the game without buying selected character. If selected character has buy button and if the player buys that character he can continue the game. First 4 characters are bought but when selected character is one of these 4 characters player cannot continue the game but he should!
if u should have used the scripts i have send you than you have to do the buy function in Characters scripts public and add this to your button becouse the bool canStart gets activated only there
public void Buy()
{
...