- Home /
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;
}
}
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
That is the correct foreach syntax. However, it still does not work.
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
Oddly, it gave errors. So I made the Item class variables static; which seems to of fixed it.
@ 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.
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.
make the GetName a public function, if that still doesn't work try making it a public static function.
You can turn that 90 to 100 as they are public. C# got them private by default. As for Boo,...well it is Boo...
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.
}
Your answer
Follow this Question
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