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 ragnaros100 · Jul 14, 2012 at 08:48 PM · c#arrayadd

Adding to an array - C#

Well It's quite simple. I was wondering if you guys knew a way to do this in C#? I've been looking on answers for a while now. But noone really had the same problem as I do.

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 whydoidoit · Jul 14, 2012 at 08:55 PM

Add a using System.Collections.Generic and use a generic List instead. Lists have array access syntax, but allow Add, Remove and Insert operations.

   List<string> myList = new List<string>();

    myList.Add("hello");


      Debug.Log(myList[0]);
Comment
Add comment · Show 9 · 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 ragnaros100 · Jul 14, 2012 at 09:07 PM 0
Share

thanks. But I get a nullrefference exception though ... Can that be because you cant add to an empty List? (I use GameObject arrays)

avatar image rutter · Jul 14, 2012 at 09:35 PM 1
Share

You can add to an empty list, but only if the list itself exists. $$anonymous$$ight be a good time to post the problematic code.

avatar image whydoidoit · Jul 14, 2012 at 09:50 PM 1
Share

You need

      selectedUnits = new List<GameObject>();
avatar image Brian Stone · Jul 14, 2012 at 09:52 PM 1
Share

I officially don't like this comment system. I'm fighting it just trying to type brackets.

You need to instantiate selectedUnits with the "new" command. Right now it's null.

private List<GameObject> selectedUnits = new List<GameObject>();

avatar image whydoidoit · Jul 14, 2012 at 09:54 PM 1
Share

Yeah it has got worse since UDN. Pretty much have to indent for code and then it doesn't always work

Show more comments
avatar image
0

Answer by ivanek333 · May 09, 2019 at 11:47 AM

Is there any function like push_back in C++ for arrays?

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 Hellium · May 09, 2019 at 11:51 AM 1
Share

Arrays in C# are not really meant to be resized repeatedly, consider using a List as suggested in the accepted answer.

If it's exceptionally, see the following links

  • https://stackoverflow.com/questions/1836959/how-to-add-to-end-of-array-c

  • https://answers.unity.com/questions/1033253/c-how-to-append-to-an-array.html

  • https://answers.unity.com/questions/57139/c-adding-items-to-an-already-created-array.html

avatar image Bunny83 Hellium · May 09, 2019 at 12:52 PM 0
Share

Right, also C++ arrays do not have a push_back method. Only vectors have and the List class in C# is the equivalent of the C++ vector type template. Note that the C++ list type is not an array but a linked list. Besides the container types there's also the native array in C++ which has no methods at all since it's just syntactic sugar for pointer arithmetic.

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

10 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

Related Questions

How to set array length in unity USING C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Adding to an Array 1 Answer

Assign a String to Each int 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