- Home /
Is it okay to assign and 'un-assign' game object tags at runtime, will it come back to bite me?
Does assigning/'un-assigning' a game object's tag have some 'scene recalculation' penalty? Or can i assign/'un-assign' tags willy-nilly?
I'm working on some interactivity between objects in the scene, and the interactivity has become a great big web. The initiation of interactions relies on identifying initiates by their tags.
The methods of 'halting' interactivity between objects during certain scenarios has reached a level of complexity that I don't want to add upon.
After some thought, the simplest way forward seems to be the simple setting/'unsetting' of tags of the interaction initiating game objects. Which would allow my current setup to easily handle enabling/disabling of any number of concurrent interactions.
AFAIK, tags are just meta-data assigned to a game object, that specify a reference word for a group of objects.
Assigning this meta-data would likely have a very $$anonymous$$imal impact on performance, so assigning tags on a large scale very rapidly, doesn't seem to have a major impact.
There is no scene recalculation penalty, I believe. When you use tag functions such as FindObjectWithTag or CompareTag, it reads the meta-data directly from the game object. The objects with certain tags aren't kept in some special list (afaik), and so there is no scene recalculations done.
You could try a stress test yourself that creates a bunch of empty objects, assigns random tags every update, and checks the time it takes to find a random object by tag, and see if the frame rate is impacted by this in any way, for example:
public class StressTest : MonoBehaviour
{
public int Entities;
public string[] Tags;
GameObject[] EntityArray;
void Update()
{
print(System.DateTime.Now.ToString());
EntityArray = new GameObject[Entities];
for (int i = 0; i < Entities; i++)
{
EntityArray[i] = new GameObject(i.ToString())
{
tag = RandomTag
};
}
var randomObj = GameObject.FindGameObjectWithTag(RandomTag);
print(System.DateTime.Now.ToString());
}
string RandomTag
{
get
{
return Tags[Random.Range(0, Tags.Length)];
}
}
}
Okay, thanks for the help brother. Cheers.
Your answer
Follow this Question
Related Questions
Caching stacked UI Image GameObject 0 Answers
find with tag VS find with name. 2 Answers
ArrayLists or Tags? 1 Answer
Best way to store large amounts of data for random generation 1 Answer
Coin Magnet Performance Issue Mobile 3 Answers