Hi all,
I’m very VERY new at C#, and as a learning assignment, I am working on an inventory system similar to WoW or other MMOs. I realise there are several tutorials on these things, but I the idea is for me to build it entirely from scratch.
Anyway, I want to have the actual Item and all its parameters separate from its reference in the Inventory system. I’ve written a very simple Item script I will attach to each object. The parameters will either be set automatically by the name of the object (The Item script will know the Apple item’s name is Apple, etc.) or I will manually set and save in a prefab (item weight, max stack size, inventory icon, etc.)
Item Code:
using UnityEngine;
using System.Collections;
public class Item : MonoBehaviour {
private string m_ItemName;
public bool m_IsStackable;
public Texture2D m_InventoryIcon;
public int m_MaxStackSize;
private void Awake () {
m_ItemName = this.name;
}
}
Yes, very bare bones for now I realise. I’m will expand on it as I go. The Inventory script is where all the item pickup stuff will go (unless you guys have a better suggestion. The issue I was running into sticking the “ive been picked up” portion on the item was OnTriggerEnter doesn’t trigger properly unless I give the player a second collider aside from his regular capsule collider).
So, when the player collides with an item, it will check to see if the item is something the player can pick up (I want to eventually distinguish between bad guys and items without having a script that just has a bool to check if the thing im checking is a baddie or an item…Any suggestions here would be helpful!) If it is, it will add the item to the inventory, and destroy the prefab in the scene.
So, my question really has to do with how Arrays work. I want the array to keep the name of the item, how many there are in each inventory spot, and the actual inventory spot it will (eventually) occupy. Is there any way to do this with a single array? I haven’t gotten into creating the Inventory GUI system, which will take the item icon designated to each item and place it in a grid depending on the order it was picked up (just like MMO bags).
I have had no luck getting my arrays to work. The error I get says “Type string[ ]' does not contain a definition for
Add’ and no extension method Add' of type
string[ ]’ could be found (are you missing a using directive or an assembly reference?)”
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour {
public string[] m_Inventory = new string[20];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider pCollider) {
if(pCollider.name == "Apple"){
m_Inventory.Add("Apple");
Destroy(pCollider.gameObject);
}
}
}
So right now just as a test to get started, I just want to add the string “Apple” to my array when the player picks up the Apple, but it’s not working…
So, I dont really want the array to hold the ACTUAL object, just the essential data required to spawn the item on the ground if the player decides to drop it. I feel that the name of the object will be enough, since i will simple instantiate a new prefab of that item name in front of the player when prompted to do so.
Any direction would be fantastic. I’m very new to programming in general so I’m attempting to do this while learning!