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.

Try using Linq:

  // UnityScript
  import System.Linq;

  ...

  var array3 = array1.Concat(array2).ToArray();

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

(uses the System namespace)

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);