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 cj31387 · May 30, 2013 at 08:51 AM · gameobjectlinkequaldatatype

How to equate (link) 2 different data types?

I've been a bit confused about this for a while now. Say you have a gameobject and a int, and you want that specific game object to be represented as a int id how would you do that?

I'm delving into coding an inventory system. So far I'm going to use a array that has 16 slots. When a player picks up a item which is a gameobject im going to need to id that and put it in the array . But here is the confusion. You cant do gameobject this = int that . So how do you make the gameobject relate to a certain int id? Hopefully someone can clear up this confusion for me. Thanks.

Comment
Add comment · Show 1
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 jerobinson · May 30, 2013 at 10:31 AM 0
Share

Not sure if this is what your looking for, so I'll comment it. Why don't you create a new array eg:

public GameObject[] inventory = new GameObject[16];

When you add a new gameobject to the array, you will be able to call it using inventory[0], [1], [2], [3] etc. Is that sort of what you were after?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by raybarrera · May 31, 2013 at 05:56 PM

You'd need an alternate way to handle the size of the inventory (easy to do), but I would recommend using a dictionary for this:

http://www.dotnetperls.com/dictionary

Just use the id as the key, and the game object as the value, so you could retrieve the item like so:

myInventoryDictionary[0]

Comment
Add comment · Show 2 · 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 cj31387 · Jun 02, 2013 at 12:23 AM 0
Share

Ok thats rad. any way to see that dictionary in the inspector?

avatar image raybarrera · Jun 02, 2013 at 12:24 AM 0
Share

Unfortunately, you can't serialize Dictionaries. But, you can have a list or array, then at runtime (probably in Awake), copy each element into the dictionary for easy access.

avatar image
1

Answer by Broheim · May 30, 2013 at 03:53 PM

keeping track of two lists (one for IDs, one for the gameobjects) is messy imo.

You can create a custom class(Item) that stores the ID and the gameobject in one place. then you just have a list of Items which you can cycle through to find corresponding GameObjects and IDs. somewhere in your code there might be something like this:

 public Item GetItemByID (int id)
 foreach (Item i in myInventory)
 {
      if (i.id == id) 
      {
           return i;
      }
      Debug.Log("Warning: Item ID not found:" + id);
      return null;
 }

 
 public int GetIDbyGO (GameObject go)
 foreach (Item i in myInventory)
 {
     if (i.go == go) 
     {
           return i.id;
     }
     Debug.Log("Warning: ID not found for gameobject " + go.name );
      return -1;
 }
Comment
Add comment · Show 4 · 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 cj31387 · May 30, 2013 at 10:48 PM 0
Share

I kind of understand that code now. But I can't add a GameObject to the myInventory list. So ill need a gameobject list to hold the items from hit.collider i picked up. That is where I don't understand how that other list will relate to yours.

avatar image Broheim · May 31, 2013 at 01:25 PM 1
Share

myInventory would be a list of "Item" (or "IdxGameObject" as posted by Vonni). You don't have any other list referencing your gameobjects. you would only work with this list. if that is not the case you would have to restructure your code to apply this concept.

avatar image cj31387 · May 31, 2013 at 05:42 PM 0
Share

Ok so this list IS the items? I kind of wanted some kind of representation in the inspector though about it. How would I do that. like i pick up a gun or something then pass it through this list how would i see it in the list in the inspector? Right now it just shows 0 array length so I don't know whats up. I cant tell you right now but i think i tried myInventory.Add(theitem) yesterday and it said 0 in the inspector and didnt show the item.

avatar image saschandroid · May 31, 2013 at 06:37 PM 0
Share

like jerobinson said: use

 public GameObject[] myInventory = new GameObject[16];

and you will see an array in the inspector with length 16 (and each GameObject in it set to NONE). If you pick up an item and put it in the list with myInventory[5] = thePickedUpGameObject in your code it should show up in the inspector. Is it that what you want?

avatar image
1

Answer by Vonni · May 30, 2013 at 05:34 PM

 // Custom Class Somewhere in your assets

 public class IdxGameObject {
 
    public GameObject go;
    public int idx;
 
    public IdxGameObject Constructor(GameObject go, int idx){
       this.go = go;
       this.idx = idx;
    }
 
 }



  // Some Script
 
  using System.Collections.Generic;
 
  public List<IdxGameObject> myList = new List<IdxGameObject>();
 
  void Start(){
      myList.Add(someGameObject, 1);
  }

  • Not sure if this is what you had in mind

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

18 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 avatar image avatar image

Related Questions

How to change the textures of assigned gameobjects? 1 Answer

How to run function in another script with prefabs? C# 2 Answers

How can I pass an instance of a gameobject to another before runtime? 1 Answer

How do I call a function in another gameObject's script? 5 Answers

How to link this 2 variables from 2 different scripts / gameobjects? 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