- Home /
How can I keep track of gameobjects getting a particular tag?
I give multiple gameobjects same tag at different points. Everytime a gameobject gets that tag, I increment the counter. Let's say, there are 5 gameobjects in the scene with the same tag and the counter is incremented to 5. Now, I need to make the first gameobject to get that tag, which incremented the counter to1 , to do something, how would I go about it? I think it is a synchronisation problem, any fixes for this?
Answer by Temseii · May 18, 2018 at 10:49 AM
Incrementing that counter will only tell you how many gameobjects you've given the tag to, you can't actually really do anything with the number. I'm not sure I understand what you're trying to do here, but what you should probably do is use something like a Generic List to hold the actual gameobjects. Here's some pseudo code that might help you out.
// Generic list with type gameobject
List<GameObject> listOfGameObjects = new List<GameObject>();
// Give the gameobject a tag and add it to the list
private void AddGameObjectToList(GameObject myGameObject) {
myGameObject.tag = "YourTagName";
listOfGameObjects.Add(myGameObject);
}
// Doing something with the gameobjects in your list (not a proper method of course, let's just use something as an example)
private void DisplayGameObjects() {
// Set every gameobject in your list to be active
for (int i = 0; i < listOfGameObjects.Count; i++) {
listOfGameObjects[i].SetActive(true);
}
// Or you could just set the first gameobject in your list to be active
listOfGameObjects[0].SetActive(true);
}
Thanks a lot for your reply. Can you please confirm that when a gameobject in the list is destroyed, the list is automatically updated, right?
No, you have to separately remove the gameobject from the list, but that's easy. Just do listOfGameObjects.Remove(GameObjectName);
where you're destroying the gameobject.
Don't forget to mark the answer if you got what you needed. :)
What if I change the tag of the gameobject which is within the list after it does what it is suppose to do, will that automatically remove that gameobject from the list? or is it once a gameobject is added in a list, it just stays there unless you explicitly remove it using: listOfGameObjects.Remove(GameObjectName);
Shouldn't make a difference, but feel free to try it out on your own. Debugging and trial & error is key.
Thanks a lot for your prompt replies and how can I make the first gameobject in the list active and all other gameobjects in the list inactive ? I tried : listOfGameObjects[0].SetActive(true); doesn't seem to be working. I just want the first gameobject in the list to do a thing, rest all should do nothing.
I am stuck at the last part, the method you suggested is working but if I setactive just the first gameobject in the list as true and others false, the true one does what it is suppose to while the false ones disappears. I want that the false ones should wait until the second gameobject in the list gets to number 1 on the list and so on.
Hope you get what I mean..
You'll need to make several modifications to the last method if you want it to do anything useful. I'm going to need to see your code to know what exactly is going wrong there. If you could upload the whole class to pastebin and just copy paste the link here I'll have a look.
Thanks a lot for the pointers. I modified the code and made it work:)
Your answer
