Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bromley · Jun 17, 2016 at 07:33 AM · errorlistinventorydontdestroyonloaditem

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bromley · Jun 17, 2016 at 05:47 PM 0
Share

Anyone help?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

53 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Problem with dropping items from my inventory 0 Answers

Item Database 3 Answers

How i can make a script-made button interactable? 0 Answers

Preventing Items from shifting in a list 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges