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
1
Question by ELISAN · Jan 23, 2014 at 08:33 AM · javascriptaddcustom classgeneric list

How to add class to a static Generic List

for writing an simple advanture inventory, I'm going to learn Generic List But, it made me crazy :( How I can add class to this static list?

 #pragma strict
 import System.Collections.Generic;
 
 static var item = new List.<itemClass>(); 
 
 class itemClass
     {
     var itemName : String;
     var itemIcon : Texture;
     var itemDescription : String;
     }
     
     item.Add(........);
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 fafase · Jan 23, 2014 at 08:34 AM

You need an instance of the class to be able to add it:

 static var item = new List.<itemClass>();
 
 function Start()
 {
     var itemToAdd :itemClass = new itemClass();
     item.Add(itemToAdd);
 }
Comment
Add comment · Show 5 · 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 ELISAN · Jan 23, 2014 at 09:13 AM 0
Share

thank you very much... it works :)

avatar image fafase · Jan 23, 2014 at 10:36 AM 0
Share

But make sure a static list is what you need. In your case, your list is static but your object are not. So when loading a new scene, those objects are referred in the list and won't be destroyed. In the end, if you store 100 items and you don't need them anymore make sure to clear your list.

avatar image ELISAN · Jan 23, 2014 at 11:26 AM 0
Share

ok, tnx... but another question: how can I use GameObject Name ins$$anonymous$$d of "itemToAdd"? Because I'm adding Class information from another object var itemToAdd :itemClass = new itemClass();

avatar image ELISAN · Jan 23, 2014 at 11:34 AM 0
Share

Or how can I set the name of new Class, in inspector?

avatar image fafase · Jan 23, 2014 at 01:02 PM 0
Share

I don't really see what you mean but I think you are trying to run before crawling. I guess you are just starting and all this is quite confusing. It will just get more confusing if you do not learn program$$anonymous$$g basics. I would recommend to start there.

You can only add references of the type that the list is declared for.

Here you declare the list for itemClass. So you the whole reference. If you are after a collection where you can use the name of the item then you want to look into Dictionary where you can couple the item name and the item itself.

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 add game object to custom class with for loop 2 Answers

Errors when trying to construct custom class in UnityScript 1 Answer

list.Add() not working 3 Answers

How to treat Input.GetAxis as Input.GetButtonDown? 9 Answers

Fade in Fade out in unity 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