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
2
Question by Saybejay · Apr 11, 2013 at 07:00 PM · c#arraysgameobjectsmerging

Is Merging GameObject Arrays Possible?

So is this possible? I have found instances where people have merged Vector3 arrays into one, but I haven't had much success in using the same coding for GameObject arrays.

I guess I should explain why I want to do this. I have created two different arrays that compile objects with two different tags. Like so:

 var array1 : GameObject[];
 var array2 : GameObject[];
 
 function Start ()
 {
    array1 = GameObject.FindGameObjectsWithTag("Tag1");
 
    array2 = GameObject.FindGameObjectsWithTag("Tag2");
 }

I would like to have both of these array's contents piled into one array though. So, array3 = array1 + array2.

I want all of the objects inside one array so it makes it easier to select the objects by clicking on them in game and just checking if they are part of array3, instead of having checks for each individual array. That is, if this is even doable.

Thanks in advance.

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 · Apr 11, 2013 at 07:01 PM

Try using Linq:

   // UnityScript
   import System.Linq;

   ...


   var array3 = array1.Concat(array2).ToArray();
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 Saybejay · Apr 11, 2013 at 11:19 PM 0
Share

I tried using this, but it keeps saying ArgumentNullException: Argument cannot be null, for the var array3. Here's what I got:

 import System.Linq;
 
 var array1 : GameObject[];
 var array2 : GameObject[];
 var array3 = array1.Concat(array2).ToArray();
 
 function Start ()
 {
    array1 = GameObject.FindGameObjectsWithTag("Tag1");
    array2 = GameObject.FindGameObjectsWithTag("Tag2");
 }

I've never used any import function before, so I have no idea what my problem is.

avatar image Saybejay · Apr 12, 2013 at 12:44 AM 0
Share

Never $$anonymous$$d. I moved the var array3 down into my function Update and now it doesn't give me that error. Thank you for the answers people!

avatar image Gaiyamato · Apr 12, 2013 at 12:56 AM 0
Share

Just as a note, the reason you had that error was that when it got to declaring array3, array1 and array2 had not yet been initialized and were both null. So you were attempting to Concat() null and null together. You can declare array3 at the top, but do the Concat() call after you have values for array1 and array2. I would also put an if statement to make sure that array1 or array2 is not null just in case.

So you would have:

import System.Linq;

 var array1 : GameObject[];
 var array2 : GameObject[];
 var array3 : Array;
  
 function Start ()
 {
 array1 = GameObject.FindGameObjectsWithTag("Tag1");
 array2 = GameObject.FindGameObjectsWithTag("Tag2");
 if(array1 && array2)
 {
   array 3 = array1.Concat(array2).ToArray();
 }
 }

You may need to put in some options to set array3 to either array1 or array2.

avatar image
5

Answer by Dracorat · Apr 11, 2013 at 07:20 PM

Without Linq, here are two ways depending on what result you want:

(uses the System namespace) http://pastebin.com/2PUqW2hV

My solution uses generics and Unity Answers keeps stripping Left-Angle-Bracket + something + Right-Angle-Bracket so I had to put it on pastebin.

 // C#
 public static void AppendSecondArrayToFirst< T >(ref T[] first, T[] second){
     int arrayOriginalSize = first.Length;
     Array.Resize< T >(ref first, first.Length + second.Length);
     Array.Copy(second, 0, first, arrayOriginalSize, second.Length);
 }
 
 public static T[] CreateCombinedArrayFrom< T >(T[] first, T[] second)
 {
     T[] result = new T[first.Length + second.Length];
     Array.Copy(first, 0, result, 0, first.Length);
     Array.Copy(second, 0, result, first.Length, second.Length);
     return result;
 }

Example Usage:

 int[] test1 = {1, 2};
 int[] test2 = {3, 4};
 AppendSecondArrayToFirst(ref test1, test2);
 
 test1 = new int[] {1, 2};
 test2 = new int[] {3, 4};
 int[] test3 = CreateCombinedArrayFrom(test1, test2);
 
Comment
Add comment · Show 6 · 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 whydoidoit · Apr 11, 2013 at 07:25 PM 1
Share

Put a space after your < Angle brackets>

avatar image Dracorat · Apr 11, 2013 at 07:27 PM 0
Share

O$$anonymous$$ that works, but it looks horrid.

avatar image whydoidoit · Apr 11, 2013 at 07:29 PM 0
Share

I know it does doesn't it - it interprets it as an HT$$anonymous$$L command otherwise.

avatar image Dracorat · Apr 11, 2013 at 08:05 PM 0
Share

I just thought to myself "self, how would you actually do this?" and I said "self, I would actually create an extension method."

So I did.

Here's with the extension method:

 public static class $$anonymous$$yExtension$$anonymous$$ethods{
     public static T[] Combine< T>(this T[] source, T[] append){
         T[] result = new T[source.Length + append.Length];
 Array.Copy(source, 0, result, 0, append.Length);
 Array.Copy(append, 0, result, source.Length, append.Length);
 return result;
     }
 }

And here's how it's used:

 int[] test3 = test1.Combine(test2);
avatar image whydoidoit · Apr 11, 2013 at 08:58 PM 0
Share

You didn't return the array so that's not how you'd call it (and if you didn't make a copy then the x = y.Combine(z) would be confusing in any case).

Show more comments

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What is wrong with my code? (Unity 2018, C#) 2 Answers

Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers

Obtaining the Variable of a Prefab in an array. 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