I’ve just finished up my first attempt at making a store for my first game but I want to have the different items in my store have different prices. Currently, all items are the same price as set in line 49. Ive been playing around with trying to make a script that sets the price that can be applied each item and sets its price that can then be used in line 49, but haven’t had much luck. If anyone could point me in the right direction that would be great!!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class StoreButtons : MonoBehaviour
{
private AudioSource source;
public int cost; //cost of the models
public List<GameObject> models;
public int selectionIndex = 0; //Default index of the models
private void Start ()
{
source = GetComponent<AudioSource> ();
selectionIndex = PlayerPrefs.GetInt ("CharacterSelected");
models = new List<GameObject> ();
foreach(Transform t in transform)
{
models.Add (t.gameObject);
t.gameObject.SetActive (false);
}
models [selectionIndex].SetActive (true);
}
public void OnClick (int index)
{
if ((DataManagement.datamanagement.modelAvailibilty & 1 << index) == 1 << index)
{
models [selectionIndex].SetActive (false);
selectionIndex = index;
models [selectionIndex].SetActive (true);
if (index == selectionIndex)
{
PlayerPrefs.SetInt ("CharacterSelected", index);
selectionIndex = index;
}
DataManagement.datamanagement.SaveData ();
}
else
{
cost = 100;
if (DataManagement.datamanagement.totalCoins >= cost)
{
DataManagement.datamanagement.totalCoins -= cost;
DataManagement.datamanagement.modelAvailibilty += 1 << index;
models [selectionIndex].SetActive (false);
selectionIndex = index;
models [selectionIndex].SetActive (true);
DataManagement.datamanagement.SaveData ();
}
else
{
source.Play ();
}
}
}
}
It seems a little odd that your using a bit flag to index your store items. Won’t that limit your to 32 or 64 items max? Also your then using that bit shifted index as an index into an array here:
That seems like its going to have a lot of empty space in the models array. Since only 1,2,4,8,16,32,… will be valid entries. models[17]->models[31] will all just be wasted empty space?
Also just to make sure you didn’t make a logical error the following if statement is ALWAYS true:
Since your setting selectionindex = index… the if statement will always be true
Finally to make the cost different you just have to set up a an array of costs for each item. How to do that will depend on how models[ ] is setup. is models[1] always the same item? or can items be inserted into the store in no particular order?
I assume models[ ] array is an array of some class? the easiest thing might be to add the cost to that class. then you could just look up models[index].cost. Without seeing exactly how the data is setup, its hard to give exact code. But the general idea is to have some array that indexes each item with a cost.
First off, thank you for your response as I am very new to programming and any feedback back helps me a ton!!
For your first statement about putting the bit shifted index as an index into an array, is there a cleaner way I should be doing this? Ive been watching tutorials on how to build an in-game store and this whole script was made though multiple different tutorials. The one where I got the bit shifting from was (at about 26:45): https://www.youtube.com/watch?v=cQHF4_YPvsM
Just to make sure I’m understanding what you said about the if statement correctly, were you just saying that basically I don’t need to say selectionIndex = index; because I had already set that above?
I changed it to this:
if (index == selectionIndex)
{
PlayerPrefs.SetInt ("CharacterSelected", index);
}
I believe this is what you were looking for when you asked how my models were set up (attachment)
My DataMangement script is using a persistantDataPath. I saw some others storing their data in PlayerPrefs. Is what I have with my store ok to use with how I have my data storage setup?
DataMangement:
public static DataManagement datamanagement;
[Space(10)]
public int currentCoins;
public int totalCoins; //Sets how much the player will have the first time they play
[Space(10)]
public int currentDistance;
public int totalDistance;
[Space(10)]
public int highScore;
public int modelAvailibilty = 1;
void Awake ()
{
if (PlayerPrefs.HasKey ("CharacterSelected"))
{
modelAvailibilty = PlayerPrefs.GetInt ("ModelAvailibilty");
}
if (datamanagement == null)
{
DontDestroyOnLoad (gameObject);
datamanagement = this;
}
else if (datamanagement != this)
{
Destroy (gameObject);
}
}
public void SaveData ()
{
BinaryFormatter binForm = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/gameInfo.dat");
gameData data = new gameData();
data.highScore = highScore;
data.totalCoins = totalCoins;
data.totalDistance = totalDistance;
data.modelAvailibilty = modelAvailibilty;
binForm.Serialize (file, data);
file.Close ();
}
public void LoadData ()
{
if (File.Exists (Application.persistentDataPath + "/gameInfo.dat"))
{
BinaryFormatter binForm = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/gameInfo.dat", FileMode.Open);
gameData data = (gameData)binForm.Deserialize (file);
file.Close ();
highScore = data.highScore;
totalCoins = data.totalCoins;
totalDistance = data.totalDistance;
modelAvailibilty = data.modelAvailibilty;
}
}
}
[Serializable]
class gameData
{
public int highScore;
public int totalCoins;
public int totalDistance;
public int modelAvailibilty;
}