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 Ruben 2 · Dec 29, 2011 at 06:17 PM · arrayobjectreferenceinventorysystem

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.

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

0 Replies

· Add your reply
  • Sort: 

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

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


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