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 Hordaland · Feb 08, 2012 at 11:42 AM · arrayinventoryweaponselement

Getting info from object within an array

I'm making an inventory system for my game. It's differs from most in the way that the items are pretty much treated like weapons are treated in FPS games. You pick an item up and the character will hold it in front of the camera while running around and you should be able to scroll through the items using the mouse wheel. The inventory system is a modified version of Xeno360's one (http://forum.unity3d.com/threads/53599-My-Contribution-Inventory-System-Free).

What I don't know is how to get the element number and name from an object in my inventory array. I have an int variable called currentWeapon that I wanna sync with the array's element's number and objectname. Ex. I have three objects: Element 0 - dagger, Element 1 - jar, Element 2 - axe. When currentWeapon == 2 I want the axe to be the "active" object.

Parts of the code:

Inventory Script:

 var Contents: Transform[];
 
 function AddItem(Item:Transform)
 {//Add an item to the inventory.
     var newContents=new Array(Contents);
     newContents.Add(Item);
     Debug.Log(Item.name+" has been added to inventroy");
     Contents=newContents.ToBuiltin(Transform);
 }

Item Script:

 function OnMouseDown()
 {//When you click an item
 var playersinv=FindObjectOfType(Inventory);//finding the players inv.
 playersinv.AddItem(this.transform);
 MoveMeToThePlayer(playersinv.transform);//moves the object, to the player
 particle.layer = 13;
 }

Thanks in advance

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
1
Best Answer

Answer by tingham · Feb 08, 2012 at 01:21 PM

You have a couple of options:

  1. If you have access to the transform then you have access to the "name" property of same so (transform.name, transform.parent.name should be illuminating.)

  2. You can keep a separate array of strings where the indexes match. Going this route can be complicated because you take responsibility for keeping the arrays in sync.

  3. Use an array that contains a Hashtable instead of just a plain old GameObject/Transform.

I'm sure there are more, these are the ones that occur to me before coffee so…

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 Hordaland · Feb 08, 2012 at 02:06 PM 0
Share

Thanks. Barging in on the transform names kinda did the job. I created two new vars (var weaponElement : Transform; var elementName : String;) and aquired the name by making these two lines: weaponElement = Contents[currentWeapon]; elementName = weaponElement.name;.

avatar image tingham · Feb 08, 2012 at 02:18 PM 1
Share

I wouldn't consider it poor-practice to utilize the data that is already exposed publicly inside of gameObject and Transform; at all. You could just use Contents[currentWeapon].name if you fancy; unless the item has the possibility of going away; in which case you should be bounds checking your array anyhow.

avatar image Hordaland · Feb 08, 2012 at 08:55 PM 0
Share

I believe I tried that but got an error telling me it couldn't make a Transform into a String or something. I'll try it the next time I do something similar though, I don't dare touching the script again after getting it to work. :)

avatar image tingham · Feb 08, 2012 at 08:57 PM 0
Share

It could be that there's some weirdness with accessing members of an array in javascript that I'm not familiar with; I mostly write C#.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you save GameObject[]? 1 Answer

Checking if items are present in array of list, how to do it? 1 Answer

type ...Material[] vs Material (materials change at run time) 1 Answer

[SOLVED] First array slot blocking second array slot 1 Answer

Error when trying to save inventory with array. 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