Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 fullje · Sep 09, 2013 at 11:41 AM · c#array

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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
     }
 }
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 fullje · Sep 09, 2013 at 01:03 PM 0
Share

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

NullReferenceException: Object reference not set to an instance of an object

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? :)

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Iterating through multidimensional arrays 2 Answers


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