- Home /
Is there an opposite of GameObject.FindGameObjectsWithTag() ?
Currently trying to implement a function that removes all children of a parent that haven't got a specific tag. I originally tried Destroy(Current_Held.FindGameObjectsWithTag(!"Placeholder_Item_1"));
To see if I could Destroy all children under the "Current_Held" parent that aren't tagged with "Placeholder_Item_1", but of course that just came up with a syntax error.
If anybody is wondering, this piece of code is being used to display the object in the hand of the player (much like Minecraft, I guess), so when the user switches what hot-bar item is selected, the code will remove the previous item out of your hand and Instantiate the new one.
If anybody has another method that would be more efficient, I'm all ears. After all, nothing is better than more efficient code.
Answer by WinterboltGames · Jul 01, 2018 at 11:23 AM
I always use this method...
public GameObject[] FindGameObjectsWithTag(string tag)
{
var sceneGameObjects = FindObjectsOfType<GameObject>();
var gameObjectsWithTag = new List<GameObject>();
for (int i = 0; i < sceneGameObjects.Length; i++)
{
var currentGameObject = sceneGameObjects[i];
if (currentGameObject.CompareTag(tag))
{
gameObjectsWithTag.Add(currentGameObject);
}
}
return gameObjectsWithTag.ToArray();
}
private void Start()
{
var gameObjects = FindGameObjectsWithTag("zombie");
foreach (var go in gameObjects)
{
Destroy(go);
}
}
It's not very efficient, because if you call FindObjectsOfType<T>()
too much your game will do hiccups and may even crash if there are too many game objects in the scene or if you call this function in Update()
--- UPDATE ---
I didn't understand your question at first (silly me!) But now I know that you are to destroy all child gameObjects that aren't tagged with a specific tag...so, use this code...
var tag = "DontDestroyMe";
var childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
var child = transform.GetChild(i).gameObject;
if (!child.CompareTag(tag))
{
Destroy(child);
}
}
The previous code will destroy all of the children that aren't tagged with DontDestroyMe.
Hope this helps...
--- UPDATE 2 ---
public class WeaponManager : MonoBehaviour
{
public GameObject[] weapons;
public void SwitchToWeapon(string weaponName)
{
if (weapons.Length == 0)
{
return;
}
for (int i = 0; i < weapons.Length; i++)
{
if (string.Compare(weapon[i].name, weaponName) == 0)
{
weapons[i].SetActive(true);
}
else
{
weapons[i].SetActive(false);
}
}
}
}
If you are trying to make a weapon switching system, do this this's much more efficient than destroying/instantiating objects everytome the player switches items...
Answer by Xaqqa · Jul 01, 2018 at 11:29 AM
@KillerOfSteam7 Thanks for the quick response and I can see why you use this method.
However, if I were to do a FindGameObjectsWithTag();
for each of the other items except the one I want to be in my player's hand, it's going to be an extremely long list, and I don't think it's going to be very good for efficiency.
If there were going to be fewer inventory items in a game, I would definitely use this code though, so I'll keep it written down!
UPDATE --
I only went a responded before I read your update, thanks so much man. I owe you one.
@$$anonymous$$illerOfS$$anonymous$$m7 If I'm only ever removing 1 child object, is it possible that I can just miss out the for loop altogether? This is what I'm thinking (Current_Held_Item will always have the tag that I want to keep).
string tag = Current_Held_Item;
GameObject child = Current_Held.transform.GetChild(0).gameObject;
if (!child.CompareTag(tag))
{
Destroy(child);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i destroy a particle when an object is destroyed 1 Answer