Add GameObject to List by tag not working consistently
Hi Guys, I am trying to do a very simple operation, which is adding GameObjects
to a list with a specific tag
, but its not working consistently across play mode. The following piece of code is in a Class which is a script attached to a Prefab.
//List to store Activated PlaceHolders.
base.activatedPlaceHolders = GetActivatedPlaceHolders(base.placeHolderLocalList);
private List<PlaceHolder> GetActivatedPlaceHolders(List<PlaceHolder> placeHolderList)
{
List<PlaceHolder> activatedPlaceHolders = new List<PlaceHolder>();
foreach (var item in placeHolderList)
{
if (item.gameObject.tag != "CollidedPlaceHolder") activatedPlaceHolders.Add(item);
}
return activatedPlaceHolders;
}
The class PlaceHolder
is attached to another Prefab
. What I am trying to do is to add the PlaceHolders whose tag is not "CollidedPlaceHolder" to the activatedPlaceHolders List. But its not working consistently when the game is played and I am not sure why. The main goal is to ignore the place holders with such a tag.
Any help would be very nice!
Answer by nico_rawitscher · Sep 05, 2018 at 05:22 PM
Hi guys, I still could not figure this out and I have tried the same logic outside of unity and it works fine.
List<int> testList = new List<int>{1,76,3,4,5,76,76,8};
public List<int> RemoveElementsB(List<int>testList)
{
List<int> a = new List<int>();
for (int i = 0; i < testList.Count; i++)
{
if(testList[i] != 76) a.Add(testList[i]);
}
return a;
}
This is a very straight foreword operation, the following method works fine, and I even tested it generating a list with the number 76 to reomove at every update call and It worked fine. So I am wondering why its not working consistently in Unity... Any help would be great, Thanks!
Your answer
Follow this Question
Related Questions
Add object of specific width to a List 1 Answer
How Do I Create a List of Two Strings? C# 1 Answer
Updating a List from a Custom Inspector 0 Answers
Passing variables from one script to all clones 1 Answer
Firebase List Users 1 Answer