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 Malwallace12 · Jul 19, 2013 at 09:11 AM · javascriptinventorypickup

I'm trying to add items to an inventory.

This scripting business is really running me ragged. I finally got my scripts to not have any errors before the game is played, but whenever I try to add an item to the inventory, I get this error.

Blockquote NullReferenceException: Object reference not set to an instance of an object

MakeGrabbable.GiveItem () (at Assets/Items/MakeGrabbable.js:53) MakeGrabbable.Update () (at Assets/Items/MakeGrabbable.js:37) I'm still fairly new to programming, so please go easy on me if it's a rookie mistake. The first two scripts are just put up so that you have an understanding of how everything is working. The only problem I have is the NullReferenceException when I try and add an item to the inventory. Item Class public class ItemClass { public var name : String; public var description : String; public var icon : Texture2D; public var itemType : String; public var maxStack : int; public var useable: Useable; enum Useable{No, Yes}
public var itemPrefab : Transform; } Inventory import System.Collections.Generic;

var playerInventory : List. = new List.(); var scrollView : Vector2; var equippedItem : ItemClass; var equipped : boolean = false; var playerHand : Transform; public var item : GameObject; function Update () { if(equippedItem.useable == ItemClass.Useable.Yes) { equipped = true; } else{ equipped = false; } } function OnGUI() { //Inventory GUILayout.BeginArea(Rect(Screen.width - 500, Screen.height - 500, 500, 500)); scrollView = GUILayout.BeginScrollView(scrollView, GUILayout.Width(500), GUILayout.Height(500)); for(var x = 0; x < playerInventory.Count; x++) { GUILayout.BeginHorizontal(); if(GUILayout.Button(playerInventory[x].icon) && playerInventory[x].useable == ItemClass.Useable.Yes && equipped == false) { equippedItem = playerInventory[x]; var UseableItem : Transform = Instantiate(playerInventory[x].itemPrefab, playerHand.position, Quaternion.identity); UseableItem.parent = playerHand; playerInventory.RemoveAt(x); return; } GUILayout.Box(playerInventory[x].name); GUILayout.EndHorizontal(); GUILayout.Box(playerInventory[x].description); GUILayout.Box(playerInventory[x].itemType); } GUILayout.EndScrollView(); GUILayout.EndArea(); //Item Equipped Slot if (GUI.Button(Rect( Screen.width/2 - 25, Screen.height - 50, 50, 50), equippedItem.icon) && equipped == true) { playerInventory.Add(equippedItem); Destroy(GameObject.FindGameObjectWithTag("Useable")); equippedItem = new ItemClass(); } } And lastly, the makeGrabbable script var ItemClass : ItemClass; var Inventory : Inventory; private var isHighlighted : boolean = false; private var gameCharacter : Transform; private var distance : float; function Start() { gameCharacter = GameObject.FindGameObjectWithTag("Player").GetComponent(Transform); Inventory = GetComponent("Inventory") as Inventory; ItemClass = GetComponent("ItemClass") as ItemClass; } function OnMouseEnter() { if(distance <= 4.0) { CreateInfoName(); isHighlighted = true; } } function OnMouseExit() { Destroy(GameObject.Find("infoName")); isHighlighted = false; } function Update () { distance = Mathf.Sqrt((gameCharacter.position - transform.position).sqrMagnitude); if(Input.GetKey("e") && isHighlighted == true){ GiveItem(); } } function CreateInfoName(){ var infoName = new GameObject("infoName"); infoName.AddComponent(GUIText); infoName.GetComponent(GUIText).text = gameObject.name; infoName.transform.position = Vector3(0.5, 0.5, 0); infoName.GetComponent(GUIText).alignment = TextAlignment.Center; infoName.GetComponent(GUIText).anchor = TextAnchor.LowerCenter; infoName.GetComponent(GUIText).pixelOffset = Vector2 (0, 25); } function GiveItem() { Inventory.playerInventory.Add(ItemClass); } I'd really appreciate any help you can give me. Some more helpful information. The inventory script is placed on the Main Camera and the MakeGrabbable script is placed on the item to be taken.
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SubatomicHero · Jul 19, 2013 at 02:37 PM

Have you tried changing the if statement to:

 if (Input.GetKey(KeyCode.e) && isHighLighted){
     GiveItem();
 }
Comment
Add comment · Show 2 · 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 Malwallace12 · Jul 20, 2013 at 12:39 AM 0
Share

Just tried, it didn't change anything. I know that the Null Reference is the variable ItemClass, but I don't understand how it could be null.

avatar image Malwallace12 · Jul 20, 2013 at 12:52 AM 0
Share

Never$$anonymous$$d, I don't understand quite how this fixed it, but what I did was put the inventory script on the item also, and it worked.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

setup a php master server 2 Answers

Need help with WeaponSwitch Script 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