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
2
Question by Jordi · Dec 31, 2010 at 09:26 PM · arraylistproperties

Creating item-packages with properties

I actually feel rather stupid asking this, but here goes.

I'm trying to create a whole array of items to be used in my game, but all items have some base properties. I want create one script with the entire list of items. Now comes the hard part: I want to be able to call in this script for a particular item and its property, much like asking gameobject.transform.position.

I realise this sounds vague, so as example: I want to find an item called "Turnip" in this list, and I want to add it to my inventory-screen. So I need to ask from the list the Turnip.associatedIcon, Turnip.SellPrice, Turnip.QuantityInInventory, etc etc.

How would I best go about this?

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

Answer by Eric5h5 · Dec 31, 2010 at 09:56 PM

Use your own class:

class Item {
    var associatedIcon : Texture2D;
    var sellPrice : int;
    var quantityInInventory : int;
}

Then make a Dictionary where the key is a string:

import System.Collections.Generic;

function Start () { var itemList = new Dictionary.<String, Item>(); itemList["Turnip"] = new Item(); itemList["Turnip"].sellPrice = 5; }

Use a constructor in the class to make it easier to work with.

Alternately, use a standard fixed array (say there are 3 different items):

var itemList = new Item[3];

Set up an enum with the names:

enum ItemName {Turnip, Apple, Bread}

Then you can refer to the entries using the enum:

itemList[ItemName.Turnip] = new Item();
itemList[ItemName.Turnip].sellPrice = 5;
Comment
Add comment · Show 8 · 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 Socapex · Dec 31, 2010 at 10:02 PM 0
Share

Interesting, so whenever you want to access an item, you would use itemList["Turnip"]. Can you do stuff like itemList["Turnip"].transform.position = Vector3(0,0,0)? Finally, how would you destroy an item in the list? itemList["Turnip"].Destroy?

I like your answer :)

avatar image Eric5h5 · Dec 31, 2010 at 10:08 PM 0
Share

If you just want a position, make a Vector3 entry in the Item class (`var position : Vector3`). If you want it to have an actual GameObject, add that (`var gameObject : GameObject`). To get rid of an entry when using a Dictionary, set it to null (`itemList["Turnip"] = null`).

avatar image Socapex · Dec 31, 2010 at 10:16 PM 0
Share

So if my class has an GameObject in it, I could do itemList["Obj"] = new Obj(); and that would create a new GameObject with the defined variables in it's class. Can you pass values to the newObj when creating? Like itemList["Obj"] = new Obj(x,y,anythingelse);

Really helpful, thanks.

avatar image Eric5h5 · Dec 31, 2010 at 11:22 PM 0
Share

Yes, as I mentioned you can use a constructor in the class definition.

avatar image Jordi · Jan 01, 2011 at 01:20 PM 0
Share

Thank you all for the superfast answers! Just one question regarding this answer: I'm very new to coding, and I was not quite sure how to create various Items in one script?

Show more comments
avatar image
1

Answer by Socapex · Dec 31, 2010 at 09:37 PM

I guess the best way is to launch a function when you are looking for an object.

Assuming Javascript and that 'name = "Turnip"'

function findObject(name : string){

 for(var obj in ObjectArray){

 if(obj.name == name){

 print("doStuffWithYourTurnip");
 print(obj.sellPrice);

 }

 else{

 print("You do not have a " + name + "in your inventory");

 }

 }

 }

Does this help?

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

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

Displaying/getting elements of a list in a certain order 3 Answers

Using List.Count on Unity iPhone 2 Answers

Getting Vector3 from random item in List 1 Answer

question about array or list of boolean 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