- Home /
inventory system issues
hello, i have been struggling with arrays recently to try and make an inventory system. here is the script i am currently working on:
 var inventory = new Array();
 var n : String;
 var d : String;
 var o : GameObject;
 var t : int;
 
 var nameString : String;
 var descriptionString : String;
 var objectString : String;
 var typeString : int;
 
 function Update () {
     if(Input.GetKeyDown("1")){
         var ItemObj : ItemClass; //Make a temporary variable
         inventory[1] = ItemObj; //Make the first slot equal to that item.
         inventory[1].name = n;
         inventory[1].description = d;
         inventory[1].object = o;
         inventory[1].type = t; //Set the properties inside the array as opposed to the variable so that if the item var is called upon, it doesn't change
     }
 }
 
 function Start (){
     inventory.length = 5;
 }
 
 function OnGUI (){ //We will use GUI in this example to display the items in the inventory and their properties, as well as the current state of
     GUI.Label(Rect(10,20,300,100),"Name: " + n); //the variables up top.    
     GUI.Label(Rect(10,40,300,100),"Description: " + d);
     GUI.Label(Rect(10,60,300,100),"Associated Object: " + o.name);
     if(t == 1) GUI.Label(Rect(10,80,100,100),"Type: Item");
     if(t == 2) GUI.Label(Rect(10,80,100,100),"Type: Weapon");
     if(t == 3) GUI.Label(Rect(10,80,100,100),"Type: Useable Item");
     
     GUI.Label(Rect(500,20,300,100),"Element Name: " + nameString);
     GUI.Label(Rect(500,40,300,100),"Element Description: " + descriptionString);
     GUI.Label(Rect(500,60,300,100),"Element Object: " + objectString);
     GUI.Label(Rect(500,80,300,100),"Element Type Number: " + typeString);
     
     //Edit 1 : Now we want to display the elements in the array. This is where we find out if the variable ItemObj
     //can provide seperate definitions for each element.
     
     if(GUI.Button(Rect(10,120,300,100),"" + inventory[1].name)){
         nameString = inventory[1].name;
         descriptionString = inventory[1].description;
         objectString = inventory[1].object.name;
         typeString = inventory[1].type;
     }
 }
I plan to add another "input.getkeydown" portion for the 2 key to fill out the second element for inventory, and so on. the error i am recieving is that the object reference is not set to an instance of an object on the line "inventory[1].name = n". var n does have a definition in the inspector. here is the class ItemClass:
 class ItemClass{
     var name : String;
     var description : String;
     var object : GameObject;
     var type : int; //1 = regular item, 2 = weapon, 3 = useable item
 
 }
Please help me out. i can't figure out why unity would tell me the variable doesnt have an instance. i have tried setting the different variables of ItemObj and then making it the first element in inventory, but i got the same error.
FIX: Changed "var ItemObj : ItemClass" to "var ItemObj = ItemClass();" and it works fine now. The problem was that ItemObj was a variable but had no definition.
Your answer
 
 
             Follow this Question
Related Questions
Help, getting ambiguous reference error 1 Answer
Inventory System: "The given key was not present in the dictionary." 2 Answers
Help, getting ambiguous reference error 1 Answer
arrays and references - a bit puzzled here 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                