• 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
0
Question by THDevelopment · Jun 17, 2014 at 01:10 PM · arrays

How to fill an array with different GameObjects?

Hello there!

Ive got a specific question maybe its a simple one, i dont know. Ive got different Objects with different tags, and i want to put them all into one array.

later - on the one hand - i want to check if there is a velocity on this objects. On the other hand i want to remove them from array, if they pass a collider. If i set up an array for all GameObjects by their own it works very well, but i want to put all the different GameObjects into one array.

Here are all the different Array i got.

     arrayGood = GameObject.FindGameObjectsWithTag("Sort_1");
 arrayBad = GameObject.FindGameObjectsWithTag("Sort_2");
 arrayClick = GameObject.FindGameObjectsWithTag("Sort_3");
 arrayRollOn = GameObject.FindGameObjectsWithTag("Sort_4");
 arrayPillow = GameObject.FindGameObjectsWithTag("Sort_5"); 

I want to put them all into one "large" array like this:

arrayAllObjects = GameObject.FindGameObjectsWithTag("Sort_1")&& GameObject.FindGameObjectsWithTag("Sort_2") && GameObject.FindGameObjectsWithTag("Sort_3") && GameObject.FindGameObjectsWithTag("Sort_4") && GameObject.FindGameObjectsWithTag("Sort_5")

But if i do it like that only the last object/array ("Sort_5") will be loaded into this array.

Can someone please tell me how i can set up this kind of array? Thank you so much!

Comment
Add comment · Show 3
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 HarshadK · Jun 17, 2014 at 01:20 PM 0
Share

Are you trying to concatenate all your arrays or want to create an array of arrays?

avatar image THDevelopment · Jun 17, 2014 at 01:29 PM 0
Share

hey harshad$$anonymous$$ i want to concatenate all the objects into one array. later the included objects and their velocity has to be checked.

avatar image HarshadK · Jun 17, 2014 at 01:32 PM 0
Share

Then check answer by @Landern below. You just need to write that code in JS ins$$anonymous$$d of C#.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by Landern · Jun 17, 2014 at 01:14 PM

You did not specify what language, in c# you can initial or set an array with the items you with.

 using System.Collections.Generic;

 GameObject[] arrayAllObjects = { GameObject.FindGameObjectsWithTag("Sort1"), GameObject.FindGameObjectsWithTag("Sort2"),  GameObject.FindGameObjectsWithTag("Sort3"),  GameObject.FindGameObjectsWithTag("Sort4"),  GameObject.FindGameObjectsWithTag("Sort_5") };
 
 // Or you can use a list c#
 
 List<GameObject> allObjects = new List<GameObject> { GameObject.FindGameObjectsWithTag("Sort1"), GameObject.FindGameObjectsWithTag("Sort2"),  GameObject.FindGameObjectsWithTag("Sort3"),  GameObject.FindGameObjectsWithTag("Sort4"),  GameObject.FindGameObjectsWithTag("Sort_5") };

// UnityScript, in this case we are using the constructor that takes an array to instantiate the list, i don't think i need to show the unityscript version of a generalized gameobject array.

    import System.Collections.Generic;
 
    var allObjects: new List.<GameObject>([GameObject.FindGameObjectsWithTag("Sort1"), GameObject.FindGameObjectsWithTag("Sort2"),  GameObject.FindGameObjectsWithTag("Sort3"),  GameObject.FindGameObjectsWithTag("Sort4"),  GameObject.FindGameObjectsWithTag("Sort_5")]);
 
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 THDevelopment · Jun 17, 2014 at 01:22 PM 0
Share

oh sorry its my first post :) i try to set it up in Java

Thank you for your fast answer!

avatar image Superfan99 · Nov 05, 2014 at 05:40 AM 0
Share

ok, so what is the js syntax for this?

avatar image srudgr8 · Dec 03, 2015 at 01:51 PM 0
Share

@Landern I tried the same in initializing an array, i got the error "Cannot implicitly convert type UnityEngine.GameObject[]' to UnityEngine.GameObject'" for the following statement.

public GameObject[] arrayAll={GameObject.FindGameObjectsWithTag("car"), GameObject.FindGameObjectsWithTag("trafficlight"), GameObject.FindGameObjectsWithTag("zebracrossing")};

What am I doing wrong here??

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert BuiltIn array to an Generic List? 1 Answer

snake problem 0 Answers

How to set parents of all objects in an array? 1 Answer

Unity c# indexoutofrangeexception: array index is out of range 1 Answer

GameObject [,] variable will it be addresses? 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