- Home /
Question by
mihairey · Nov 19, 2017 at 04:28 PM ·
scripting problemplayerassetshop
How to activate Selected Character in MainLevel?
Hello guys,
I have the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Boomlagoon.JSON;
public class Inventory : MonoBehaviour {
public static Inventory Instance { get; private set; }
private JSONObject itemsData;
public static int SelectedItemIndex { get; private set; }
public class ShopItem
{
public bool Selected, Bought;
public int Price;
public string Name;
public ShopItem( bool bought, bool selected, int price, string name)
{
Bought = bought; Selected = selected; Price = price; Name = name;
}
}
public static List<ShopItem> Items;
public static int coinsAmount;
private void Awake()
{
Instance = this;
if (!PlayerPrefs.HasKey("Items"))
{
PlayerPrefs.SetString("Items", "{\"Items\":[{\"name\":\"StandardFX\",\"bought\":true,\"selected\":true,\"price\":1},{\"name\":\"SuperFX\",\"bought\":false,\"selected\":false,\"price\":5},{\"name\":\"UltimateFX\",\"bought\":false,\"selected\":false,\"price\":5}]}");
}
if (PlayerPrefs.HasKey("CurrentMoney"))
{
coinsAmount = PlayerPrefs.GetInt("CurrentMoney");
}
else
{
coinsAmount = 0;
PlayerPrefs.SetInt("CurrentMoney", 0);
}
itemsData = JSONObject.Parse(PlayerPrefs.GetString("Items"));
Items = new List<ShopItem>();
for (int i = 0; i < itemsData.GetArray("Items").Length; i++)
{
Items.Add(new ShopItem(itemsData.GetArray("Items")[i].Obj.GetBoolean("bought"),
itemsData.GetArray("Items")[i].Obj.GetBoolean("selected"),
(int)itemsData.GetArray("Items")[i].Obj.GetNumber("price"),
itemsData.GetArray("Items")[i].Obj.GetString("name")));
if (Items[i].Selected)
{
SelectedItemIndex = i;
}
}
}
public void SelectItem(int index)
{
for(int i = 0; i < Items.Count; i++)
{
if (Items[i].Selected)
{
Items[i].Selected = false;
itemsData.GetArray("Items")[i].Obj.GetValue("selected").Boolean = false;
}
}
Items[index].Selected = true;
itemsData.GetArray("Items")[index].Obj.GetValue("selected").Boolean = true;
SelectedItemIndex = index;
PlayerPrefs.SetString("Items", itemsData.ToString());
PlayerPrefs.Save();
}
public void BuyItem(int index)
{
if (SubtractCoins(Items[index].Price))
{
Items[index].Bought = true;
itemsData.GetArray("Items")[index].Obj.GetValue("bought").Boolean = true;
SelectItem(index);
}
}
private bool SubtractCoins(int value)
{
if(coinsAmount - value < 0)
{
return false;
}
coinsAmount -= value;
PlayerPrefs.SetInt("CurrentMoney", coinsAmount);
return true;
}
}
// How can I activate the GameObject that has the boolean "selected" == true to MainLevel? I have like 3 GameObject (3 sprites for the main character - it is a 2d game) and i want one to be active and the other two to be deactivated ( depending on what user selects in the shop).
Can you help me?
Comment
Your answer
Follow this Question
Related Questions
Why do the player's health points go down so fast? 1 Answer
asset wont accept attached script 0 Answers
ReImport in c# of GameObjects only for Scene Objects, not assets? 1 Answer
Unity Character Selection + store 1 Answer
Player Sprite Changing Scenes And getting a new camera to follow it and default spawn position 1 Answer