- Home /
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.
Answer by whydoidoit · Apr 11, 2013 at 07:01 PM
Try using Linq:
// UnityScript
import System.Linq;
...
var array3 = array1.Concat(array2).ToArray();
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.
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!
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.
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);
I know it does doesn't it - it interprets it as an HT$$anonymous$$L command otherwise.
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);
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).