- Home /
Inventory = How to add items :) C-Sharp
Hi, im trying to create an inventory system to my game. But i have problem with add items to my inventory. it is sad for me :(
I've got:
In inventory.cs:
float[,] plecak = new float[4,4]; // tablica, there are polish so ;) there is a two-dimensional array = plecak.
and simple GUI slots
for(int i = 0; i < 4; i++)
{
for(int k = 0; k < 4; k++)
{
GUI.BeginGroup(new Rect(12,15,300,300));
GUI.Box (new Rect(5+(i*42),5+(k*42),40,40),":" +plecak[k,i]);
GUI.EndGroup();
}
}
On Item.cs i've go:
private GameObject Gracz;
private float odleglosc;
public Texture2D ikona;
void Start()
{
Gracz = GameObject.FindWithTag("Player");
}
void Update()
{
}
void OnMouseDown()
{
odleglosc = (transform.position - Gracz.transform.position).sqrMagnitude;
if(odleglosc <= 4f)
{
print("Jestes blisko.");
//Here!!!!
Destroy(gameObject);
}
}
Item on the ground have Tag = Item.
And, there where is "//Here!!!" i need some code to pick up items :( could u help me? Please?
Answer by aldonaletto · Sep 09, 2013 at 12:08 PM
Are you trying to increment the corresponding plecak element when picking up some item? If so, you should specify in the item script which's its element index - doing this with a public variable is the easiest way. Furthermore, the item must get a reference to the script inventory.cs - you could Find the object it's attached to at Start. Supposing that this object is named "InventoryObject":
public Texture2D ikona;
public int rowNum; // set the row number for this item
public int colNum; // set the column number for this item
private GameObject Gracz;
private float odleglosc;
private inventory invent;
void Start()
{
Gracz = GameObject.FindWithTag("Player");
// find the object named InventoryObject and get reference to the script inventory.cs
invent = GameObject.Find("InventoryObject").GetComponent<inventory>();
}
void OnMouseDown()
{
odleglosc = (transform.position - Gracz.transform.position).sqrMagnitude;
if (odleglosc <= 4f)
{
print("Jestes blisko.");
invent.plecak[colNum, rowNum] += 1; // increment element in the inventory
Destroy(gameObject);
}
}
aldonaletto, thanks for this but i've got problem. I show u my scripts.
Item.cs
using UnityEngine;
using System.Collections;
public class Item : $$anonymous$$onoBehaviour {
private GameObject Gracz;
private float odleglosc;
private ekwipunek invent;
public Texture2D ikona;
public int rowNum; // set the row number for this item
public int colNum; // set the column number for this item
void Start()
{
Gracz = GameObject.FindWithTag("Player");
invent = GameObject.Find("InventoryObject").GetComponent<ekwipunek>();
}
void Update()
{
}
void On$$anonymous$$ouseDown()
{
odleglosc = (transform.position - Gracz.transform.position).sqr$$anonymous$$agnitude;
if(odleglosc <= 4f)
{
print("Jestes blisko.");
invent.plecak[colNum, rowNum] += 1; // increment element in the inventory
Destroy(gameObject);
}
}
void OnGUI()
{
}
}
And, ewkipunek.cs // its inventory : >
using UnityEngine;
using System.Collections;
public class ekwipunek : $$anonymous$$onoBehaviour {
public Texture2D texture;
public Rect windowSize = new Rect(30,30,200,200);
public float[,] plecak = new float[4,4]; // tablica
// Update is called once per frame
void Start(){
}
void Update ()
{
}
void OnGUI()
{
windowSize = GUI.Window(0, windowSize, eq, "Ekwipunek");
}
void eq(int id)
{
// Graficzny ekwipunek i wyswietlanie zawartosci komórki
for(int i = 0; i < 4; i++)
{
for(int k = 0; k < 4; k++)
{
GUI.BeginGroup(new Rect(12,15,300,300));
GUI.Box (new Rect(5+(i*42),5+(k*42),40,40),":" +plecak[k,i]);
GUI.EndGroup();
}
}
GUI.DragWindow();
}
}
When i added ur code and set row and col, i ve got error here : invent.plecak[colNum, rowNum] += 1; // increment element in the inventory
Item.On$$anonymous$$ouseDown () (at Assets/Item.cs:36) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32) And if its not problem for you, could you help me? :)NullReferenceException: Object reference not set to an instance of an object
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Set Current Array GameObject to Active 1 Answer
How to check for an empty array? 1 Answer