- Home /
Question by
ceme4ko4532 · Apr 25 at 11:24 AM ·
listvariablefloat
How to set value from list to objects 1 by 1
so in RefreshUI() i wanted to set values from FloatDatabase to ooo's when they instantiate and do it 1 by 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class InventorySystem : MonoBehaviour
{
[SerializeField] public List<Item> ItemsInInv = new List<Item>();
[SerializeField] public List<float> FloatDatabase = new List<float>();
//public List<ItemSlot> Slots = new List<ItemSlot>();
public Transform itemParent;
public ItemSlot Slot;
public RefObj refr;
ItemSlot ooo;
// where should the inventory be saved?
private string savePath { get { return Path.Combine(Application.persistentDataPath, "inventory.json"); } }
// Itms currently in the inventory
[SerializeField] public List<Item> Itms;
//[SerializeField] public List<float> Flts;
//[SerializeField] public List<ItemSlot> Slts;
public InventorySystem()
{
Itms = new List<Item>();
//Flts = new List<float>();
//Slts = new List<ItemSlot>();
}
// save the inventory to disk
public void Save()
{
Itms = ItemsInInv;
//Flts = FloatDatabase;
//Slts = Slots;
File.WriteAllText(savePath, JsonUtility.ToJson(this));
}
// load the inventory from disk
public void Start()
{
if (File.Exists(savePath))
{
JsonUtility.FromJsonOverwrite(File.ReadAllText(savePath), this);
ItemsInInv = Itms;
//FloatDatabase = Flts;
//Slots = Slts;
}
}
public void RefreshUI()
{
itemParent = refr.itempar;
Slot = refr.slt;
foreach (Transform child in itemParent)
{
GameObject.Destroy(child.gameObject);
}
foreach (Item Itm in ItemsInInv)
{
ooo = Instantiate(Slot, itemParent);
ooo.slotItem = Itm;
ooo.RefreshSlot();
}
Save();
}
}
Comment
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Public and Static Variables 2 Answers
Variables from one script to another. 3 Answers
how do i make a var list thing 1 Answer
Random Float Variable 2 Answers