- Home /
Perfect inventory code giving errors? [C#]
I can't understand why this code is giving me so many errors, it seems perfectly fine to me but yet i'm getting errors on lines :
26
30
32
33
53
54
62
Can someone please help me figure out the problem?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventorySys : MonoBehaviour
{
// Remade the code and took out most of the comments sense everything is self-explanitory -- to keep it sexy..
public List<Items> Inventory;
public Items[] playerInventory;
public Rect inventoryWindow = new Rect (0,0,600,600);
private RaycastHit drug;
private Ray lord;
void Awake ()
{
Inventory = new List<Items> (); // Needed to be initialized.
}
void Update ()
{
Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetButtonDown("Fire1")) { // If object on ground is clicked on -->
if (Physics.Raycast(Ray,out drug, 20)) {
for (int x = 0; x < playerInventory.Length; x++) { // Check the player's entire inventory to see if it already has the item -->
if (drug.collider.tag == playerInventory[i].itemID.ToString()) {
Inventory.Add(playerInventory[i]); // Adding to our inventory.
Destroy(drug.collider.gameObject); // Once the player picks up the object, we destroy the object from the floor.
}
}
}
}
}
// Really simple GUI code for testing purposes.
void insideInventory (int windowID) {
int expandRec = 20;
GUI.DragWindow(new Rect(0,0,600,20));
for (int i = 0; i < Inventory.Count; i++){// Finding everything in solid inventory. The player inventory is for grabbing objects and fowarding them into the solid inventory.
GUI.Button(new Rect(20, expandRec, 64, 65), Inventory[i].icon); // Creates small buttons with our icons on them.
GUI.Label(new Rect(70, expandRec, 64, 65), Inventory[i].name);
GUI.Label(new Rect(90, expandRec, 64, 200), Inventory[i].description);
GUI.Button(new Rect(100, expandRec, 64, 65), "Power Level : " + Inventory[i].itemPowerLevel.ToString() + "Damage!");
// You get the point.
}
}
void OnGUI() {
InventoryWindow = GUI.Window(0, inventoryWindow, insideInventory, "Inventory Menu by XX!");
}
}
which is from a class like this :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Items {
public string itemName;
public string itemDescription;
public int itemPowerLevel;
public int itemDamage;
public int itemRange; // Don't know if this was needed, but whatever. EX : Kirito's Bow - Range : 500-600.
public int itemID;
public int itemPrice;
public GameObject attachObject;
public Texture2D icon; // Attaching an icon.
public Transform itemPrefab; // Prefab for item.
public enum ItemGroup{ Weapon, Clothing, Potion, Quest, Recipe, MescItem } // Can't think of anything else.
public ItemGroup itemCategory;
// Constructors.
/**
public Items_X InvetoryCon = new Items_X(0, 0, 0, 0, 0); // Instance.
public Items_X(int d, int r, int u, int g, int s)
{
itemPowerLevel= d;
itemRange = r;
// ^ add on, if needed.
}
**/
}
26 >> Ray is a class, not a variable | 32 >> don't you have already class named Inventory ? I would suggest not using a capital first letter to variable.
Answer by Bunny83 · Nov 25, 2013 at 02:35 PM
"Perfect inventory code"... That's a joke, right? :D
The code has very inconsistent nameing. Some variables starts with a capital letter (which should start with a lower case letter) and some Methods start with a lowercase letter (which should be upper case).
in line 22 you try to assign a Ray to the Ray class which doesn't make any sense. You might wanted to assign it to "lord".
Just a followup: lord? drug? d, r, u, g, s? Worst possible names here. Variable names should tell you something about the use of a variable.
In line 26 you again pass the class Ray where you should pass an instance of the class Ray. (lord? can you hear me?).
A class that represents an item should be named "Item", not "Items".
Your "Items" class doesn't have a "name" or a "description". You called them itemName and itemDescription, which is btw redundant. You should rename the variables and remove the "item" from the names.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122715im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
error CS1061 anybody might help me? 1 Answer
I am having a problem with the following script 1 Answer
Scrolling inventory script giving argument out of range. 2 Answers
c# Weapon Pickup Script 1 Answer