Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 valax · Apr 10, 2013 at 10:33 AM · errorarrayinventoryreturn

Returning item names in an inventory array

I'm trying to go through the current items in an inventory and return their names (Inventory.ListItems()). The rest of the script works apart from this piece. An error is returned (Object reference not set to an instance of an object) for some reason.

InventorySystem.js:

 var item : Item;
 var inventory : Inventory;
 
 class Inventory {
 
     private var size : int = 20;
     private var occupiedSlots : int = 0;
     
     private var items : Item[] = new Item[size];
     
     function ListItems () {
     
         for(var i = 0; i < occupiedSlots; i ++){
         
             Debug.Log(items.GetName(items[i])); //Line where it errors at.
         }
     }
     
     function AddItem (item : Item) {
         
         if(occupiedSlots < size){
             
             items[occupiedSlots + 1] = item;
             occupiedSlots ++;
         }
         
         else{
         
             Debug.Log("Inventory full");
         }
     }
 }
 
 class Item {
 
     var icon : Texture2D;
     var name : String;
     var description : String;
     
     function GetName (item : Item) : String{
     
         return item.name;
     }
 }
 
 function Start () {
 
     inventory = new Inventory();
     
     item = new Item();
     item.name = "test";
     item.description = "A test item";
     
     inventory.AddItem(item);
 }
 
 function Update () {
 
     if(Input.GetKeyDown(KeyCode.Q)){
     
         inventory.AddItem(item);
     }
     
     if(Input.GetKeyDown(KeyCode.E)){
     
         inventory.ListItems();
     }
 }

Thanks for reading.

If you want to use this code feel free to do so. Just please reference that is made by me (Sam Farhan).

Fix:

 function AddItem (item : Item) {
     
         if(occupiedSlots < size){
     
             items[occupiedSlots] = item;
             Debug.Log("Added " + item.name + " to slot " + occupiedSlots);
             occupiedSlots ++;
         }
     
         else{
     
             Debug.Log("Inventory full");
         }
     }

And:

 class Item {
  
     var icon : Texture2D;
     var name : String = null;
     var description : String = null;
     
     function GetName (item : Item) : String{
     
         return name;
     }
 }
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 Fattie · Apr 10, 2013 at 10:52 AM 0
Share

suggest use List, not arrays

3 Replies

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

Answer by fafase · Apr 10, 2013 at 10:38 AM

 function ListItems () {
    for(var i = 0; i < items.Length; i ++){ 
        Debug.Log(items[i].name); 
    }
 }

or

 function ListItems () {
    for(var obj :Item in items){ 
        Debug.Log(obj.name); 
    }
 }

Actually not totally sure of the foreach syntax in Js

Comment
Add comment · Show 9 · 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 valax · Apr 10, 2013 at 10:54 AM 0
Share

That is the correct foreach syntax. However, it still does not work.

avatar image luckruns0ut · Apr 10, 2013 at 11:00 AM 0
Share

His will work if you change the var to be public.

avatar image fafase · Apr 10, 2013 at 11:02 AM 0
Share

Yep there is one ) too many on the Debug (I removed it now) I just tested it and it does work for me. I get a whole bunch of "test" and "null" if not totally full

avatar image valax · Apr 10, 2013 at 11:25 AM 0
Share

Oddly, it gave errors. So I made the Item class variables static; which seems to of fixed it.

avatar image fafase · Apr 10, 2013 at 12:15 PM 0
Share

@ valax what errors do you get? Because, I just copy/paste your code with my functions and I get it perfect so you may have something else?

@luckrunsOut making the variable public is not the problem here since the function is public, it is accessible and the function can itself access private members of the same class.

Show more comments
avatar image
0

Answer by luckruns0ut · Apr 10, 2013 at 10:35 AM

Debug.Log(items.GetName(items[i])); is incorrect. Use this:

Debug.Log(Item.GetName(items[i])); and tell me whether it works.

Comment
Add comment · Show 4 · 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 valax · Apr 10, 2013 at 10:42 AM 0
Share

This doesn't work. :/

avatar image luckruns0ut · Apr 10, 2013 at 11:06 AM 0
Share

make the GetName a public function, if that still doesn't work try making it a public static function.

avatar image valax · Apr 10, 2013 at 11:25 AM 0
Share

In JS I'm %90 sure that variables are public by default.

avatar image fafase · Apr 10, 2013 at 12:42 PM 0
Share

You can turn that 90 to 100 as they are public. C# got them private by default. As for Boo,...well it is Boo...

avatar image
0

Answer by Dracorat · Apr 10, 2013 at 06:14 PM

 Debug.Log(items[i].GetName()); //Line where it errors at.

Also note that GetName in the Items class is wrong. Since it represents one item, you don't need to send an item in.

 function GetName () : String{
 
    return name;
 }

Finally, "item" isn't defined in the Update function:

 if(Input.GetKeyDown(KeyCode.Q)){
    inventory.AddItem(item); // You need to create an item called "item" before you can attempt to add it.
 }
Comment
Add comment · 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

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

13 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

Related Questions

When using input, array.Add replaces the last member 1 Answer

List array in GUI.Box 0 Answers

Texture2d[]: Array index is out of rang (Javascript) 3 Answers

Save an array to file 1 Answer

Inventory moving items bug. 0 Answers


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