x


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

asked Jun 05 '10 at 12:04 AM

Cap gravatar image

Cap
311 8 9 18

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

answered Jun 05 '10 at 12:44 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

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.

Jun 05 '10 at 02:55 AM Cap
(comments are locked)
10|3000 characters needed characters left

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

answered Jun 05 '10 at 12:22 AM

qJake gravatar image

qJake
11.6k 43 78 161

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.

Jun 05 '10 at 12:35 AM Eric5h5

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... >_>

Jun 05 '10 at 08:05 AM qJake

I also posted this somewhat late, and probably hadn't thought it through entirely. :P

Jun 05 '10 at 08:06 AM qJake
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x56
x9
x5

asked: Jun 05 '10 at 12:04 AM

Seen: 1476 times

Last Updated: Jun 05 '10 at 12:04 AM