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 mimatos · Jan 12, 2011 at 10:09 PM · arrayinbetweeninsert

Add Value into Array between two elements

Well just as the title reads, I want to know if it is possible adding a new value into an array but instead of just building onto it I want to be able to pinpoint a current element (which I already have) and insert a new value into there while pushing the value that use to be there and all the remaining values after it up one level. So for example:

Original Array: "Mike","John","Dan","Steve";

I click on John which gives me it position and insert a new value pushing all the values after it up one.

     New Array: "Mike","Arthur","John","Dan","Steve";

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

Answer by yoyo · Jan 12, 2011 at 10:34 PM

This is a general programming question, not really Unity. You didn't specify the language, so I'm going to assume C# (to improve my odds of a correct answer ;) for which the System.Array docs on MSDN are relevant.

Arrays are fixed length structures that do not support the dynamic insertion of new entries. If you want to insert into an array, you need to write the code to do it yourself.

A simple (but inefficient) way is something like this, assuming entries are strings, as in your example ...

List<string> mylist = new List<string>(myarray);
mylist.Insert(index, mynewstring);
myarray = mylist.ToArray();

This works because List does support dynamic insertion, and there are helper methods to convert between lists and arrays.

Note that you'll need to add "using System.Collections.Generic;" at the top of your script.

Anyone else want to take a crack at the javascript version?

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 mimatos · Jan 13, 2011 at 12:58 AM 0
Share

Thanks a lot for your answer and sorry about being vague on the program$$anonymous$$g language. I am actually using Javascript. I am not that great at C# so is there like a Javascript equivalent to what you have done here?

avatar image yoyo · Jan 14, 2011 at 03:33 AM 0
Share

This answer should help with translation ... http://answers.unity3d.com/questions/35939/help-on-translating-this-c-to-javascript

avatar image
-1

Answer by Justin Warner · Jan 12, 2011 at 10:33 PM

Check out ArrayList's, you can use the .add command to add in an object... I do think ArrayList's only use objects, but not 100. Check this for more info. Best I can do. Hope it helps!

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 yoyo · Jan 12, 2011 at 10:35 PM 1
Share

As of .NET 2.0 it's generally preferable to use List -- all the benefits of ArrayList, plus type-safety.

avatar image Justin Warner · Jan 12, 2011 at 11:10 PM 0
Share

I'm going by what we've learned in school, right now were doing objects with ArrayList's so that's why I mentioned it...

avatar image yoyo · Jan 13, 2011 at 02:50 AM 0
Share

ArrayList is a valid approach, just a little dated ... not sure it deserves the downvote.

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

How to check if a specific gameobject is in the array? 1 Answer

Array - insert item in the middle 1 Answer

unityscript equivalent of javascript splice? 1 Answer

deleting a face of a proceduraly created mesh. 1 Answer

Array of Strings and keyword "in" 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