Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by twoxsu · Jul 14, 2016 at 09:00 PM · arrayinventoryinventory system

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image twoxsu · Jul 14, 2016 at 09:10 PM 0
Share

yes its hard coded inventory system. ima gonna try this solution. and buy a new keyboard perhaps.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to go around creating inventory with new GUI (4.6 Beta)? 1 Answer

Which is better for an inventory system, Array or List? 0 Answers

How to start an inventory system? 2 Answers

Scriptableobject List and Instantiating objects from it 3 Answers

Inventory Drop Function Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges