I am using Vuforia in Unity which has a built in script called DefaultTrackableEventHandler. It has a code like this;
protected virtual void OnTrackingFound()
{
var rendererComponents = GetComponentsInChildren<Renderer>(true);
foreach (var component in rendererComponents)
component.enabled = true;
}
I have some items with the tag "ignoreRend" which I do not want to be rendered when the tracking finds the target image. I have a list like this:
GameObject[] ignoreTheseObjects = GameObject.FindGameObjectsWithTag("ignoreRend");
I've been trying to find a way to make the foreach loop ignore the items in my ignoreTheseObjects list without any success. It feels like something that would be easy to code but I'm stuck... Is there any way to compare the items in the lists? I've tried searching for answers but haven't found anything that is suitable for this problem. I'm thinking something like;
if (rendererComponents[i] == ignoreTheseObjects[i])
.. but not sure how to write it further. Any help would be appreciated!
In addition to Leo Bartkus answer:
Assuming that the items you want to ignore, are also part of your rendererComponents collection, you can just do something like the following:
Just check for the tag in the loop.
foreach (var component in rendererComponents)
{
if (component.tag == "ignoreRend")
{
continue; //this will continue with next item in list
}
component.enabled = true;
}
As Leo suggested you can also use LINQ
In order to use LINQ you have to import it by
using System.LINQ
Then you can use it like:
var filteredComponents = rendererComponents.Where(r => r.gameObject.tag != "ignoreRend")
foreach (var component in filteredComponents)
{
component.enabled = true;
}
For more information on LINQ u may want to visit getting started with LINQ