- Home /
Best way to concat builtin arrays?
Incase there's an altogether better way to do this, here's the situation. I have two groups of objects in my scene, tagged ships and driftwood. The player should be able to target objects in both groups via a keypress so I need to Concat 2 builtin arrays of GameObjects into a single array which I can use GetComponent on later. At the moment I'm using this code to change the arrays from builtin to JS ones, Concat them, and then change them back (can I use GetComponent on a JS array? If so, ignore the last line).
Is this the best way to do this, or is there some easier way to have FindGameObjectsWithTag grab objects with more than one tag?
var allaships = GameObject.FindGameObjectsWithTag ("ship");
var allshipsjs = new Array (allaships);
var alldriftwood = GameObject.FindGameObjectsWithTag ("driftwood");
var alldriftwoodjs = new Array (alldriftwood);
var concated = allshipsjs.Concat(alldriftwoodjs);
var allships : GameObject[] = concated.ToBuiltin(GameObject);
Answer by Eric5h5 · Jun 05, 2010 at 12:44 AM
You can't pass more than one tag to FindGameObjectsWithTag. You can use GetComponent with any game objects in a JS array; everything in such an array is an Object, so you can cast it to whatever type you need. I'm not entirely sure what you're using the arrays for in the first place--it doesn't seem to me that you would need arrays for targeting an object--but I don't know how your code is set up, so maybe you do need them. :)
As far as your actual code itself goes, you can shorten it somewhat:
var allshipsjs = new Array (GameObject.FindGameObjectsWithTag ("ship") );
var alldriftwoodjs = new Array (GameObject.FindGameObjectsWithTag ("driftwood") );
var allships = allshipsjs.Concat(alldriftwoodjs).ToBuiltin(GameObject);
Okay, thanks for the clarification and the shorter code, I'll just use a JS array and skip the last conversion then.
I'm populating the arrays with every ship and driftwood object (cargo etc) in the scene at any particular time. Since objects in there are spawned dynamically, I can't just reference them directly. Using an array means I can have a keydown to target the next and previous objects in a reliable fashion. The order doesn't matter provided it doesn't change too often. I'll be reusing this same array for a number of things too, so I think it should work well.
Answer by qJake · Jun 05, 2010 at 12:22 AM
That's probably the best way, but you've got a lot of extra random variables that you don't need to be set (I blame UnityScript's confusing dynamic variable typing for this). You can shorten this to this:
var allaships = GameObject.FindGameObjectsWithTag ("ship");
var alldriftwood = GameObject.FindGameObjectsWithTag ("driftwood");
var allships = allships.Concat(alldriftwood);
There's no dynamic typing in Cap's code, it's all static. You may be confusing dynamic typing with type inferencing (which, by the way, is exactly the same thing you can do in C# 3.0, so don't diss it. ;) ). Your code won't work because you can't Concat built-in arrays like GameObject[], you can only Concat Javascript arrays.
What the hell is the difference? You know what, don't even bother, I'm just going to add this to the list of things I hate about the shitty implementation of JavaScript syntax onto the .NET CLR. Stupid UnityScript...die in a fire... >_>
I also posted this somewhat late, and probably hadn't thought it through entirely. :P
Your answer
Follow this Question
Related Questions
Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers
Get parent/root objects in scene? 1 Answer
FindGameObjectsWithTag alternative for better perfomance 1 Answer
How to find a certain Gameobject using a variable, in a common script from an array of gameobjects? 2 Answers
How to call Class specific methods from an object stored in a GameObject[]? 1 Answer