- Home /
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.
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}Blockquote NullReferenceException: Object reference not set to an instance of an object
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.
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();
}
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.
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
Follow this Question
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