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
0
Question by Triqy · Nov 07, 2013 at 06:45 PM · arraygameobjectslistsfind

Gather all GameObjects with a certain name in a Scene and Apply them to a list?

Gather all GameObjects with a certain name in a Scene and Apply them to a list or Array?

Any help would be great! thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by clunk47 · Nov 07, 2013 at 08:55 PM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GOList : MonoBehaviour 
 {
     List<GameObject> goList = new List<GameObject>();
     string nameToAdd = "Cube";
     
     void Start()
     {
         foreach(GameObject go in GameObject.FindObjectsOfType(typeof(GameObject)))
         {
             if(go.name == nameToAdd)
                 goList.Add (go);
         }
         
         print (goList.Count);
     }
 }
 
Comment
Add comment · Show 8 · 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 Triqy · Nov 08, 2013 at 05:21 AM 1
Share

Thanks $$anonymous$$an! I can always count on you for good answers. Works perfectly.

avatar image clunk47 · Nov 08, 2013 at 05:27 AM 1
Share

Always happy to help :D

avatar image clunk47 · Nov 08, 2013 at 09:27 PM 1
Share

Try something like this:

 using UnityEngine;
 using System.Collections;
 
 public class ArrayExample : $$anonymous$$onoBehaviour 
 {
     public GameObject[] goArray;
     public string nameToArray = "Cube";
     
     void Start()
     {
         GameObject[] temp = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
         int count = 0;
         
         foreach(GameObject go in temp)
         {
             if(go.name == nameToArray)
                 count++;    
         }
         
         goArray = new GameObject[count];
         
         for(int i = 0; i < goArray.Length; i++)
         {
             if(temp[i].name == nameToArray)
                 goArray[i] = temp[i];
             goArray[i].name = nameToArray + " - " + i.ToString();
         }
         
         temp = null;
         
         foreach(GameObject go in goArray)
         {
             print (go.name);    
         }
     }
 }
 
avatar image Triqy · Nov 09, 2013 at 10:56 AM 1
Share

Never $$anonymous$$d! Got it to work. lol here is my code converted into JS.

Basically what this does is (in execution order) -

-Creates buildings/objects on start - -finds all buildings in scene - -finds specific name type buildings - -Puts newly created buildings in new Array - -Numbers the buildings from 1 to 10 for later use - -nulls/clears out the AllObjects array-

 import System.Collections.Generic;
 
 var AllObjects: GameObject[];
 var $$anonymous$$ediumCommericalBuildings: GameObject[];
 var $$anonymous$$ediumCommerical_New : Transform;
 
 var Name = "$$anonymous$$ediumCommerical_New";
 
 function Start () {
 //Object creation
     for (var A : int = 0;A < 10; A++) {
         $$anonymous$$ediumCommerical_New = Instantiate ($$anonymous$$ediumCommerical_New, Vector3(A * 4.0, 0, 0),transform.localRotation);
         $$anonymous$$ediumCommerical_New.name = Name;     
 }
 
 //Find All Objects 
 
 AllObjects = FindObjectsOfType(GameObject);
 
 //Creates New Array According to name specifc buildings in AllObjects and creates the correct amount of elements for that Array
 var count: int = 0;
 var GO: GameObject;
 
 for(GO in AllObjects){    
     if(GO.name == Name){
     count ++;
 }
 }
 
 $$anonymous$$ediumCommericalBuildings = new GameObject[count];
 
 //Pushes the new Buildings in New Array 
 for(var i = 0; i < $$anonymous$$ediumCommericalBuildings.Length; i++){
 
 if(AllObjects[i].name == Name){
 
 $$anonymous$$ediumCommericalBuildings[i] = AllObjects[i];
 $$anonymous$$ediumCommericalBuildings[i].name = Name + "_" + i.ToString();
 
 }
 }
 
    AllObjects = null;
 
 }
     function Update() {
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             var hit: RaycastHit;
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, hit)) {
                 if (hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_0" 
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_1"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_2"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_3"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_4"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_5"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_6"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_7"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_8"
                 || hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_9"){
                 
                       Destroy(hit.collider.gameObject);
                 
 }
 }
 }
 }

  

Thanks a lot for your help!

avatar image clunk47 · Nov 09, 2013 at 07:00 PM 1
Share

No problemo dude. Thanks for posting your JS version for others to see.

Show more comments
avatar image
2

Answer by shadows_s · Nov 07, 2013 at 07:21 PM

it is can be helpful: http://answers.unity3d.com/questions/24257/how-do-i-find-all-game-objects-with-the-same-name.html

Comment
Add comment · 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
1

Answer by zombience · Nov 07, 2013 at 07:14 PM

you could use Linq if you're using C#

 using System.Linq;
 
 GameObject[] objs = GameObject.FindObjectsOfType(typeof(GameObject)).Select(g => g as GameObject).Where(g => g.name == "desired name").ToArray();


or, if you wanted it to be put in a list, you would do the exact same thing except create a List and end your Linq Query with .ToList() rather than .ToArray()

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 zombience · Nov 07, 2013 at 07:15 PM 1
Share

this is something you should not do every frame. Linq tends to be slow and will bog down performance if overused.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Array problems 2 Answers

Change List name as the "Name" Property in the list 1 Answer

How to create an array out of many gameObjects? 1 Answer

Multiple Fire Points 2 Answers

How you find an Object 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