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 Kourosh · Apr 18, 2011 at 08:23 PM · arrayvector3listcombine

How to combine Vector3 arrays?

Here is an example in JS:

var vArray:Vector3[]; var positions:Vector3[];

function AddToArray(){ //Add positions to the vArray. }

How would I combine two arrays like this? A single array out of two arrays.

Any hint is very much appreciated.

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

3 Replies

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

Answer by Eric5h5 · Apr 18, 2011 at 09:23 PM

You can use System.Array.Copy:

function CombineVector3Arrays (array1 : Vector3[], array2 : Vector3[]) : Vector3[] {
    var array3 = new Vector3[array1.length + array2.length];
    System.Array.Copy (array1, array3, array1.length);
    System.Array.Copy (array2, 0, array3, array1.length, array2.length);
    return array3;
}
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 Bunny83 · Apr 18, 2011 at 09:32 PM 0
Share

Well, it's a bit shorter than my first solution and i guess a bit more efficient than my second ;) Does System.Array.Copy even care about the containing type or is it blind (object) copying?

avatar image Eric5h5 · Apr 18, 2011 at 09:52 PM 0
Share

@Bunny83: http://msdn.microsoft.com/en-us/library/system.arraytypemismatchexception.aspx Aside from being shorter, System.Array.Copy is a little more efficient than doing it yourself.

avatar image
4

Answer by Bunny83 · Apr 18, 2011 at 08:49 PM

Arrays can't be resized so you have to create a new array that is big enough to hold the elements of both arrays.

var vArray:Vector3[]; var positions:Vector3[];

function AddToArray(){ var tmpArray = new Vector3[vArray.length + positions.length]; var i = 0; for (var V : Vector3 in vArray) { tmpArray[i++] = V; } for (var V : Vector3 in positions) { tmpArray[i++] = V; } vArray = tmpArray; }

But with a List it would be simpler

var vArray:Vector3[]; var positions:Vector3[];

function AddToArray(){ var tmpArray = new List.<Vector3>(vArray); tmpArray.AddRange(positions); vArray = tmpArray.ToArray(); }

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 Eric5h5 · Apr 18, 2011 at 09:53 PM 1
Share

Careful about putting C# syntax into JS examples. ;)

avatar image Bunny83 · Apr 18, 2011 at 10:34 PM 0
Share

xD damn, found it, i will change it :D

avatar image
0

Answer by Wibbs 1 · Apr 18, 2011 at 08:29 PM

I'm not at my home computer so can't check whether this exactly right, but it should give you an idea...

var i:int;

for (i=0; i<vArray.Count; i++) { vArray[i] += positions[i]; }

If += doesn't work then change the line to:

vArray[i] = vArray[i] + positions[i];
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 Kourosh · Apr 18, 2011 at 08:44 PM 0
Share

Thank you. I've tried your first suggestion it I get the this error: NullReferenceException.

And your second suggestion will add values. What i'm after is to make a single array out of two.

avatar image Wibbs 1 · Apr 18, 2011 at 08:52 PM 0
Share

Ah sorry, I didnt realise that was what you meant.

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

Getting Vector3 from random item in List 1 Answer

A node in a childnode? 1 Answer

How to prevent picking the same combination in array? 1 Answer

Create an array of Vector3 from Editor script 1 Answer

Get size of a vector3 array c# 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