- Home /
DontDestroyOnLoad make errors
I have 2 scripts: first one is a script attached on all items of my scene and adds these on inventory; second one is the inventory system script attached on a gameobject with all inventory scripts (like database, toolips and go on).
Here the first:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using LitJson;
public class ItemData : MonoBehaviour {
public int ID;
public enum itemIndex { MainEntranceKey = 0, Flashlight = 1, Medikit = 2}
public itemIndex itemNameFromList;
public float maxDistance = 5.0F;
public Ray raycast;
public bool pickedUp = false;
private int itemID;
private int itemsJsonIndex;
public string itemState;
private GameObject glob;
private Inventory_System invDat;
private Global_ItemInventory globScript;
private PlayerInteractivity inter;
private IEnumerator fetchInventory;
void Start ()
{
pickedUp = false;
fetchInventory = LoadItemDatabase ();
StartCoroutine (fetchInventory);
}
IEnumerator LoadItemDatabase ()
{
glob = GameObject.Find ("GlobalParameters");
while (glob == null) {
yield return null;
}
globScript = glob.GetComponent <Global_ItemInventory> ();
inter = GameObject.Find ("PlayerController").GetComponent<PlayerInteractivity> ();
GameObject inv = GameObject.Find ("Inventory");
while (inv == null) {
yield return null;
}
Item_Database itemDat = inv.GetComponent<Item_Database> ();
invDat = inv.GetComponent<Inventory_System> ();
while (itemDat == null) {
yield return null;
}
itemDat.itemList = JsonMapper.ToObject (File.ReadAllText (Application.dataPath + "/StreamingAssets/Items.json"));
itemDat.items.Add (new Item ((int)itemDat.itemList [itemsJsonIndex] ["id"], itemDat.itemList [itemsJsonIndex] ["name"].ToString (), itemDat.itemList [itemsJsonIndex] ["desc"].ToString (), itemDat.itemList [itemsJsonIndex] ["raw"].ToString(), (int)itemDat.itemList [itemsJsonIndex] ["type"]));
if (itemNameFromList == itemIndex.MainEntranceKey) {
itemsJsonIndex = 0;
} else if (itemNameFromList == itemIndex.Flashlight) {
itemsJsonIndex = 1;
} else if (itemNameFromList == itemIndex.Medikit) {
itemsJsonIndex = 2;
}
itemID = itemDat.items[itemsJsonIndex].ID;
StopCoroutine (fetchInventory);
}
void Update ()
{
RaycastHit hit;
raycast = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
if (Physics.Raycast (raycast, out hit, maxDistance) && hit.transform.name == name)
{
Debug.DrawLine (raycast.origin, hit.point, Color.red);
if (Input.GetButtonDown ("Interact") && inter.interactKeyEnable == true)
{
pickedUp = true;
TakeItem ();
}
}
}
void TakeItem ()
{
if (invDat.itemObj.Exists (x => x.ID == -1))
{
invDat.AddItem (itemID);
ItemStat it = globScript.itemSt.Find (x => x.ID == ID);
int k = globScript.itemSt.FindIndex (x => x.ID == ID);
if (it != null) {
globScript.itemSt.Remove (it);
globScript.itemSt.Insert (k, (new ItemStat (ID, "Destroyed", name)));
} else {
globScript.itemSt.Add (new ItemStat (ID, "Destroyed", name));
}
Destroy (this.gameObject, 0.0F);
}
else
{
Debug.Log ("FULL");
}
}
}
And the second:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory_System : MonoBehaviour {
public int maxSlots;
public GameObject inventorySlot;
public GameObject inventoryIcon;
public List<Item> itemObj = new List<Item> ();
public List<GameObject> slots = new List<GameObject> ();
private GameObject inventoryPanel;
private GameObject slotPanel;
private Item_Database database;
void Start ()
{
database = GetComponent<Item_Database> ();
inventoryPanel = GameObject.Find ("InvPanel");
slotPanel = inventoryPanel.transform.FindChild ("InvSlot").gameObject;
for (int i = 0; i < maxSlots; i++)
{
itemObj.Add (new Item());
slots.Add (Instantiate (inventorySlot));
slots [i].transform.SetParent (slotPanel.transform);
slots [i].transform.localScale = new Vector3 (1, 1, 1);
}
}
public void AddItem(int id)
{
Item itemToAdd = database.FetchItemByID (id);
for (int i = 0; i < itemObj.Count; i++)
{
if (itemObj [i].ID == -1)
{
itemObj [i] = itemToAdd;
GameObject objects = Instantiate (inventoryIcon);
objects.GetComponent<SelectionTool> ().item = itemToAdd;
objects.transform.SetParent (slots[i].transform);
objects.transform.localScale = new Vector3 (1, 1, 1);
objects.transform.localPosition = Vector2.zero;
objects.GetComponent<Image> ().sprite = itemToAdd.Sprite;
slots [i].name = itemObj [i].Name;
objects.name = itemToAdd.Name;
break;
}
}
}
public void LoadData(int max, int[] id)
{
maxSlots = max;
for (int i = 0; i < maxSlots; i++)
{
if (id [i] != -1)
{
AddItem (id [i]);
}
}
}
}
If I add the DontDestroyOnLoad () function in the inventory system script (and I would to do that), the first script ItemData gives me this error:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Item].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) ItemData+c__Iterator8.MoveNext () (at Assets/Script/ItemData.cs:69) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) ItemData:Start() (at Assets/Script/ItemData.cs:38)
But if I don't put that function, these scripts works very well, but I need that function...
What am I doing wrong?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Problem with dropping items from my inventory 0 Answers
Item Database 3 Answers