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:01 AM · gameobjectarraytagtagsindex

Need a Custom array Index to look up gameobject tags?

I need to create a index of a lot of tags so i can use this to look up these tags very easy.

Comment
Add comment · Show 1
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 Azrapse · Nov 07, 2013 at 06:07 AM 1
Share

Explain more. What do you want to achieve with this? If you only need to have a list of tags, any List< string > will help. If you need something more advanced, you will have to explain yourself.

2 Replies

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

Answer by clunk47 · Nov 07, 2013 at 06:52 AM

If you want to grab all gameObjects in the scene and add their tags to a List:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class FindTags : MonoBehaviour 
 {
     List<string> tags = new List<string>();
     
     void Start()
     {
         foreach(GameObject go in GameObject.FindObjectsOfType (typeof(GameObject)))
             tags.Add (go.tag);
         foreach(string tag in tags)
             print (tag);
     }
 }
 




If you want to name the tags manually:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TagList : MonoBehaviour 
 {
     string[] temp;
     List<string> tags = new List<string>();
     
     void Start()
     {
         temp = new string[]{"Tag1", "Tag2", "Tag3", "Tag4"};    
         tags.AddRange (temp);
         if(tags.Contains(tag))
             print ("This gameObject's tag index is: "+tags.IndexOf(this.tag));
         else
             print ("This gameObject's tag does not exist in your list!");
     }
 }
 
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 Hoeloe · Nov 08, 2013 at 10:23 AM 1
Share

I do not recommend using lists when arrays will serve. Arrays are smaller and faster, but don't allow dynamic lengths.

avatar image Azrapse · Nov 08, 2013 at 10:55 AM 1
Share

Lists are internally using an array. They are basically arrays stuffed with utility methods.

avatar image Hoeloe · Nov 08, 2013 at 12:04 PM 1
Share

Yes, but the wrapper has an overhead, and because of they allow dynamic length, initialisation is slower.

avatar image Azrapse · Nov 08, 2013 at 12:08 PM 1
Share

That could make sense if we were talking about using thousands of lists, or tens of lists being created per frame.

For just one list that is going to last for the length of the game time, the overhead is negligible compared to the features it provides.

Initialization is as slow as you want it to be. If you know the initial amount of elements, you can set the size for the internal array in the constructor. Check this overload.

avatar image Hoeloe · Nov 08, 2013 at 12:53 PM 2
Share

This is all true, but you can get all the same features on native arrays using Linq, except for dynamic length - in general, arrays are better for this kind of use. If you don't need the dynamic length, it's good practice to use arrays, even if the performance difference is negligible. They are generally better for this purpose. You don't have to worry about internal representation when initialising, for example.

Show more comments
avatar image
1

Answer by Huacanacha · Nov 07, 2013 at 06:07 AM

You may want a hash map like Dictionary in C#. Check it out here: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

I assume you want fast lookup from tags to the GameObects with those tags, in which case you probably want something like:

 Dictionary<string, List<GameObject>>

Edit: changed GameObject to List

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

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

19 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 avatar image avatar image

Related Questions

finding an objects index in a multi dimensional static array? 1 Answer

setting the tag of all gameObjects in an array 1 Answer

Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers

How to create an array of Game Objects with different tags. 1 Answer

Find and store gameObjects in a array 2 Answers


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