- Home /
Problem with arrays (inventory system) array not declared, will remain null or something like this
(sorry for everything lowercase, the shift on my keyboard is kinda stuck(u know how hard its to script that way?))
lately i decided to implement simple inventory system in my game. i created a simple image in paint.net that resembles thing i wanted to have. then i applied it to canvas. for every slot in this inventory i made a raw image with collider, and a counter thats a child of the image. works fine so far. then i made a script for it like this(not done yet)
using UnityEngine;
using System.Collections;
public class inwentarz : MonoBehaviour {
public GameObject eq; // whole inventory object
public bool czyEqWidoczne = false;
public bool czyMyszWolna = false;
public Texture[] itemyTeksturki; // item textures
private Texture[] itemyWEqtekstury; //textures of items that are in inventory now
private GameObject[] ObiektIkonyItemu; // array with image objects
private int[] itemyWequ; // the inventory itself
private string TerazPrzypisuje; //so that you can assign textures to arrrays
// ITEM & TEXTURE ids 0-nic 1-lom 2-pistolet 3-aks74u 4-jagody 5-kokosy 6-banany 7-magazynkiMakarow 8-magazynkiAKS74u 9-LeczniczeLiscie
void Start(){
for(int i = 0; i < 20; i++)
{
TerazPrzypisuje = "Item (" + i.ToString() + ")";
ObiektIkonyItemu[i] = GameObject.Find("Canvas/Inwentarz/" + TerazPrzypisuje);
itemyWEqtekstury[i] = ObiektIkonyItemu[i].GetComponent<Renderer>().material.mainTexture;
itemyWequ[i] = 0;
}
//Debug Items
itemyWequ[1] = 4;
itemyWequ[3] = 9;
}
void FixedUpdate () {
//Cursor.visible = true;
if(Input.GetKeyDown(KeyCode.Tab))
{
switch(czyEqWidoczne)
{
case false:
czyEqWidoczne = true;
eq.SetActive(true);
czyMyszWolna = true;
break;
case true:
czyEqWidoczne = false;
eq.SetActive(false);
czyMyszWolna = false;
break;
}
}
switch(czyMyszWolna)
{
case false:
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
break;
case true:
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
break;
}
for(int i = 0; i < 20; i++)
{
if(itemyWequ[i] != 0)
{
itemyWEqtekstury[i] = itemyTeksturki[i];
}
}
}
}
the game compiles and runs, but the debug items does not show in the inventory screen and some weird errors appear
NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object)
and i dont want to use pre-made inventory systems, because i know i wont learn anything that way. cheers!
Answer by vintar · Jul 14, 2016 at 09:07 PM
I`m assuming your error points to line 19? Its because you have not initialized the array with a length. Theres two things you can do judging by your for loop using 20 I assume that is a hard coded inventory limit? Either 1)
private Texture[] itemyWEqtekstury = new Texture[20];
private GameObject[] ObiektIkonyItemu = new GameObject[20];
or 2) make these arrays public and drag the objects onto the inspector.
yes its hard coded inventory system. ima gonna try this solution. and buy a new keyboard perhaps.
Answer by twoxsu · Jul 15, 2016 at 07:59 PM
I dont know how but i got it to working.And i deleted some unnecessary variables. this might make my script work slower, but hey, nobody pays me for making good optimized scripts ;p Here it is:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class inwentarz : MonoBehaviour {
public GameObject eq; // caly obiekt inventory
public bool czyEqWidoczne = false;
public bool czyMyszWolna = false;
public Texture[] itemyTeksturki; // Tekstury itemow ktore ustawiasz w inspektorze, by mogly sie pokazac w eq
public GameObject[] ObiektIkonyItemu = new GameObject[20]; // by mozna bylo dostac sie do licznikow itemow
private int[] itemyWequ = new int[20]; // wlasciwe eq na podstawie ktorego beda wykonywane inne akcje
// 0-nic 1-lom 2-pistolet 3-aks74u 4-jagody 5-kokosy 6-banany 7-magazynkiMakarow 8-magazynkiAKS74u 9-LeczniczeLiscie
void Start(){
for(int i = 0; i < 20; i++)
{
itemyWequ[i] = 0;
}
//Debug Itemy
itemyWequ[0] = 4;
itemyWequ[1] = 9;
itemyWequ[2] = 3;
}
void FixedUpdate () {
//Cursor.visible = true;
if(Input.GetKeyDown(KeyCode.Tab))
{
switch(czyEqWidoczne)
{
case false:
czyEqWidoczne = true;
eq.SetActive(true);
czyMyszWolna = true;
break;
case true:
czyEqWidoczne = false;
eq.SetActive(false);
czyMyszWolna = false;
break;
}
}
switch(czyMyszWolna)
{
case false:
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
break;
case true:
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
break;
}
for(int i = 0; i < 20; i++)
{
if(itemyWequ[i] != 0)
{
Debug.Log("Wykryto nowy item w inventory! jego id to " + itemyWequ[i].ToString() + "natomiast slot to "+i.ToString());
ObiektIkonyItemu[i].GetComponent<RawImage>().texture = itemyTeksturki[itemyWequ[i]-1];
}
}
}
}
Honestly i had more luck doing this that i thinked it out myself. Especially this line
ObiektIkonyItemu[i].GetComponent().texture = itemyTeksturki[itemyWequ[i]-1];
was rather a desperate experiment than something thinked out. Anyways,thanks for help.And i need to get myself new keyboard.
Your answer
