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 nick597 · Jun 23, 2019 at 09:18 AM · arrayscript.inventory

Array for items (Inventory) Not Working

Array for items (Inventory) Not Working Until I Inspect the object with the script attached

Here is the script am using.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 
 
 
 public class Farmer : MonoBehaviour {
     
     
 [System.Serializable]
  public class Inventory {
     public string ItemName;
     public int ItemQuantity;
     public bool StackAble;
     public GameObject ItemGameObject;
  }
     private int SlotUse;
     private bool CanHit;
     private Animation EquipedAnim;
 
     public AllItems _AllItems;
     public Transform Grabber;
     public int GrabberDistance;
 
     public Item Droppeditem;
     public Texture BlankSlotImage;
 
     public GameObject Tool_Holder;
     public GameObject RaycastStart;
     public GameObject EquipedTool;
 
     public bool EquipTool;
     public bool GrabbingTool;
     public bool BuildingMode;
 
     public BuildingChecker _BuildingChecker;
 
     public Inventory[] inventory;
 
     RaycastHit hit;
 
     
 
 
 
     void Start () {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
         EquipTool = false;
         GrabbingTool = false;
         CanHit = true;
         BuildingMode = false;
         inventory = new Inventory[6];
     }
 }


Error

No Error

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 Kennai · Jun 23, 2019 at 12:41 PM 0
Share

I suppose the reason is because your Inventory class is inside Farmer class. $$anonymous$$ove that class out of Farmer class (lets say, write it after Farmer class).
And remove that line from start if you dont want to override inspector values

 inventory = new Inventory[6];

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jun 23, 2019 at 01:40 PM

You still need to learn a lot about object oriented programming ^^. Yes Unity can sometimes be a bit misleading because it does a lot things for you you may not be aware of. In this particular case you have marked your "Inventory" class as Serializable. Therefore Unity will serialize the class in the editor and show it inside the inspector as nested instances. The important thing here is: Who and when are all the objects we deal with created? Well your "Farmer" class instance is created when you attach it to a gameobject, either in the editor or by using AddComponent from a script.


Usually objects do not create themselfs magically. However the Unity inspector does some of that magix for you. For example your public inventory array in your Farmer class is automatically created by the inspector. When you change the size of the array in the inspector, the inspector will actually create a new array and copy the old elements over into the new array (since arrays can not be resized). Likewise because your Inventory class is serializable the inspector will create an instance of your Inspector class for every element in your array. All those created objects are serialized by the inspector and loaded again when you start your game.


However when you create objects manually, Unity will not create anything for you. This line:

 inventory = new Inventory[6];

replaces the previously serialized array with a new array with 6 elements. However those 6 elements are all null. Keep in mind this is a new array with 6 elements of a reference type. So the array only contains references to Inventory instances. Though you never create any Inventory instance. You essentially have two options here:


Either create the Inventory instances inside the inspector by setting the size of the array to 6 and remove the line inventory = new Inventory[6]; from your code. That way you get the array as well as the 6 Inventory class instances automatically from the serialization system when the gameobject is deserialized.


Your other option is to manually create the object as it is common in any "normal" C# application

 inventory = new Inventory[6];
 for(int i = 0; i < inventory.Length; i++)
 {
     inventory[i] = new Inventory();
 }

Comment
Add comment · Show 1 · 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 nick597 · Jun 24, 2019 at 06:59 AM 0
Share

Thank you so much for your time and Thanks for explaining what was the problem I appreciate that now it works fine.

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

139 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 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 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

How to determine what type of class is being interated through in an array of multiple classes 1 Answer

How to display a list of methods and allow a choice of one of them 1 Answer

Getting info from object within an array 1 Answer

,If i were to have a inventory system could i split it twice 0 Answers

How can I do that in an array with gameobject.findobjectoftype but that does not count the same object in which this script is? 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