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
1
Question by Cap · Jun 05, 2010 at 12:04 AM · arrayfindgameobjectswithtagjoinconcat

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);
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
1
Best Answer

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);
Comment
Add comment · Show 1 · 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 Cap · Jun 05, 2010 at 02:55 AM 0
Share

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.

avatar image
-2

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);
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 Eric5h5 · Jun 05, 2010 at 12:35 AM 0
Share

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.

avatar image qJake · Jun 05, 2010 at 08:05 AM 0
Share

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

avatar image qJake · Jun 05, 2010 at 08:06 AM 0
Share

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

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

No one has followed this question yet.

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


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