- Home /
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.
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.
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!");
}
}
I do not recommend using lists when arrays will serve. Arrays are smaller and faster, but don't allow dynamic lengths.
Lists are internally using an array. They are basically arrays stuffed with utility methods.
Yes, but the wrapper has an overhead, and because of they allow dynamic length, initialisation is slower.
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.
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.
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
Your answer
Follow this Question
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