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 haha0512 · Apr 07, 2017 at 11:23 PM · scripting problemarrayinventory

One component in Array which is a class resets itself to null.

I don't even know how to formulate a question since I have 0 idea why this error is occurring. My observation, after 5 quadrillion Debug.Log statements, is that somewhere along storing the class array which includes class, the class value becomes null. I have checked this through confirming whether or not the values actually change. It's better explained through code.

Inventory.cs: handles inventory storage as well as GUI display.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;

 public class Inventory : MonoBehaviour {

     public ItemClass holdingNow;
     inventoryItems[] inventoryStuff = new inventoryItems[5];
     public GameObject[] itemIconGUI = new GameObject[5];
     public GameObject[] itemNumberGUI = new GameObject[5];

     // Use this for initialization
     void Start () {
         Debug.Log ("start");
         inventoryStuff [0] = new inventoryItems (null, "", 0);
         inventoryStuff [1] = new inventoryItems (null, "", 0);
         inventoryStuff [2] = new inventoryItems (null, "", 0);
         inventoryStuff [3] = new inventoryItems (null, "", 0);
         inventoryStuff [4] = new inventoryItems (null, "", 0);
     }
     
     // Update is called once per frame
     void Update () {
         //update gui display
         for (int i = 0; i < 5; i++) {
             Debug.Log ("updating GUI");
             itemIconGUI [i].GetComponent<Image> ().sprite = Sprite.Create (inventoryStuff [i].iClass.itemTexture, new Rect (0, 0, 30, 30), new Vector2 (0.5f, 0.5f));

             itemNumberGUI [i].GetComponent<Text> ().text = inventoryStuff [i].numberOfItem.ToString ();
         }


     }

     public void AddItem (ItemClass itemClass) {
         for (int i = 0; i <= 4; i++) {
             Debug.Log (inventoryStuff [i].iClass + ", " + inventoryStuff [i].item + ", " + inventoryStuff [i].numberOfItem);
         }
         //already exists and stackable
         for (int i = 0; i <= 4; i++) {
             if (inventoryStuff [i].item == itemClass.itemName) {
                 Debug.Log (itemClass);
                 if (inventoryStuff [i].numberOfItem < itemClass.maxStack) {
                     inventoryStuff [i].iClass = itemClass;
                     inventoryStuff [i].numberOfItem += 1;
                     Debug.Log ((ItemClass)inventoryStuff [i].iClass);
                     return;
                 }
             }
         }
         int firstEmptySlot = emptySlot ();
         if (firstEmptySlot >= 0) {
             inventoryStuff [firstEmptySlot].iClass = (ItemClass)itemClass;
             inventoryStuff [firstEmptySlot].item = itemClass.itemName;
             inventoryStuff [firstEmptySlot].numberOfItem = 1;
             return;
         }
         if (firstEmptySlot < 0) {
             Debug.Log ("inventory full");
             return;
         }
     }

     int emptySlot() {
         for (int i = 0; i <= 4; i++) {
             if (inventoryStuff [i].item == "") {
                 return i;
             }
         }
         return -1;
     }

     public class inventoryItems {
         public ItemClass iClass;
         public string item;
         public int numberOfItem;

         public inventoryItems(ItemClass cla, string ite, int num) {
             iClass = cla;
             item = ite;
             numberOfItem = num;
         }
     }
 }

ItemInteraction.cs: handles interactions between player and item tagged objects.

 using UnityEngine;
 using System.Collections;

 public class ItemInteraction : MonoBehaviour {

     private Vector3 position;
     public GameObject inventoryManager;

     void Start() {
         position = new Vector3(Screen.width/2, Screen.height/2);
     }
     
     void Update () {
         if(Input.GetKeyDown(KeyCode.E) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) {
             Collect ();
         }
     }

     void Collect () {
         Ray interactRay = Camera.main.ScreenPointToRay (position);
         RaycastHit interactionInfo;
         if (Physics.Raycast (interactRay, out interactionInfo, 100)) {
             GameObject collectableObject = interactionInfo.collider.gameObject;
             if (collectableObject.tag == "item") {
                 Destroy (collectableObject);
                 inventoryManager.GetComponent<Inventory> ().AddItem (collectableObject.GetComponent<ItemClass> ());
                 Debug.Log (collectableObject.GetComponent<ItemClass> ());
             }
         }
     }
 }

ItemClass.cs: base class for items.

 using UnityEngine;
 using System.Collections;

 public class ItemClass : MonoBehaviour {

     public string itemName = "item";
     public int maxStack = 30;
     public Texture2D itemTexture;

     public virtual void Use() {

     }

     public virtual void Trash() {

     }
 }



I suspect that the storing of the data type ItemClass is where everything is going terribly wrong, though I don't see why because the class array's class specifies the ItemClass.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 08, 2017 at 01:10 AM

Well, your ItemClass is a component. Components can only exist on gameobjects. Your problem is that you destroy the gameobject that contains the ItemClass component which you add into your list. When the gameobject get destroyed all components on that gameobject are destroyed as well. References to destroyed components become "fake-null" objects.

Comment
Add comment · Show 3 · Share
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 haha0512 · Apr 08, 2017 at 04:55 AM 0
Share

How would I address this issue then? Because I think its important for my script to have ItemClass as a part of the array.

avatar image haha0512 · Apr 08, 2017 at 05:05 AM 0
Share

Is there a way to store it without the script needing to be attached to a gameobject?

avatar image Bunny83 haha0512 · Apr 08, 2017 at 10:23 AM 0
Share

No, it's not. Your concept simply doesn't work that way. You basically have two options:

  • Use an item class that is not a component. Either use a ScriptableObject or a normal class. That instance can be referenced by a component script on the phyical item in the scene.

  • If you want to use the item as component you must not destroy the object. You can deactivate the object or just disable certain components and maybe parent the object to a container object on your player. That way the object is no longer visible but still exists .

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't Assign Item In Array 1 Answer

Calling an object from Array/List 1 Answer

Cant fetch components from GameObjects stored in an array? 0 Answers

Convert Inventory from JS to C# (problem with list) 2 Answers

New Gui 4.6 - How to use new GUI for inventory system 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