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 Tpaley · Sep 18, 2013 at 12:10 AM · c#arrayclassinventoryadd

C# Adding to an Array

I have this problem in C# where when I try to add an item to an array, it simply makes the array blank. No error is produced.

My code for adding an item:

 public void AddItem ( string itemName  ,   int qty ,   string type  ){
     
     Item itemToAdd = new Item();
     itemToAdd.name = itemName;
     itemToAdd.quantity = qty;
     itemToAdd.type = type;
     ArrayList itemsResizeableArray = new ArrayList();
     //Debug.Break();
     if ( items != null && items.Length != 0 )
     {
         itemsResizeableArray = new ArrayList( items );
     }
     itemsResizeableArray.Add( itemToAdd );
     items = itemsResizeableArray.ToArray() as Item[];
 }

I would rather not use Lists because I would need to change a large portion of my Unity Project. I create the Array here:

 public Item[] items;

and declare the class:

 [System.Serializable]
 public class Item
 {
     public string name = "";
     public int quantity = 1;
     public string type = "";
 }

I would like help making my function actually add an Item to the array.

Thanks in advance.

Comment
Add comment · Show 5
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 Eric5h5 · Sep 18, 2013 at 12:13 AM 4
Share

Just use a generic List (not ArrayList, that's obsolete). The changes needed are $$anonymous$$imal and your project will be much better ins$$anonymous$$d of trying to hack arrays to do things they're not meant for.

avatar image clunk47 · Sep 18, 2013 at 12:20 AM 0
Share

Just to add to Eric5h5's comment, have a look at System.Collections.Generic.

avatar image AndyMartin458 · Sep 18, 2013 at 12:25 AM 0
Share

Which line is it that is deleting your array? Isn't line 7 and 11 pretty redundant?

avatar image clunk47 AndyMartin458 · Sep 18, 2013 at 12:30 AM 0
Share

Please don't post comments as answers.

avatar image Tpaley · Sep 18, 2013 at 12:55 AM 0
Share

Line 14 is causing the problem.

1 Reply

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

Answer by ArkaneX · Sep 18, 2013 at 07:50 AM

ArrayList.ToArray() returns object[]. You can't cast it to Item[], so using 'as' keyword causes items to be null again. To be able to accomplish this, you can change your line to:

 items = itemsResizeableArray.ToArray(typeof(Item)) as Item[];

But, please DON'T do this, and instead change your variable definition to

 public List<Item> items;

as @Eric5h5 suggested. With this change, instead of calling AddItem("x", 1, "y"), you can simply do

 items.Add(new Item
 {
     name = "x",
     quantity = 1,
     type = "y"
 });

or you can add proper constructor to your Item class, and just call

 items.Add(new Item("x", 1, "y"));

You'd need to modify your code of course, but I strongly suggest doing it. Even if you use this variable in 100 lines, most of them won't require any change, and you can fix all in a matter of minutes.

Comment
Add comment · Show 3 · 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 Tpaley · Sep 18, 2013 at 09:20 PM 1
Share

I guess I'll have to transition to lists. Currently I'm transitioning to C# from UnityScript and I'm trying to keep everything pretty much exactly as I did it before until I finish converting my scripts. Thanks for the help!

avatar image Devsignerz · May 21, 2017 at 07:23 AM 0
Share

i don't think you can use Add() in C# it didn't work for me

avatar image Eric5h5 Devsignerz · May 21, 2017 at 07:38 AM 0
Share

Of course you can use Add() in 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

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

Related Questions

Error passing class in method c# 3 Answers

Writing an class array to disk? 1 Answer

[CLOSED] When I use this code that I made, it adds to the item catalogue too? [CLOSED] 1 Answer

Can't Assign Item In Array 1 Answer

Inventory AddItem help 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