Question About LIST

I have a List Script that has a two items inside one of them is the GameObject and another one is mesh renderer;

[System.Serializable]
public class ShadowList
{

    public GameObject shadow;
    public MeshRenderer meshrener;

    public ShadowList(GameObject newshadow, MeshRenderer newmesh)
    {
        shadow = newshadow;
        meshrener = newmesh;
    }
  

}

so what i wanted to do and i dont know how can i find out if any of mymesh renderer is visible or not!
before that i was using array and it was pretty simple like this:

    for (int i = 0; i < ObjectRenderers.Length; i++) {
            if (ObjectRenderers[i].isVisible) {
                transform.SetParent(targetImageObjects[i].transform.parent.gameObject.transform, false);

now i want to write it with list but i dont know how can i make it:
here is my code so far:

public class TrackableTeleporter : MonoBehaviour {

    public List<ShadowList> newList = new List<ShadowList>();

    private GameObject[] targetImageObjects;
    private MeshRenderer[] ObjectRenderers;


    // Use this for initialization
    void Start () {

        targetImageObjects = GameObject.FindGameObjectsWithTag ("teleportTracker");

        ObjectRenderers = new MeshRenderer[targetImageObjects.Length];
        for (int b = 0; b < targetImageObjects.Length; b++) {
            newList.Add(new ShadowList(targetImageObjects [b],targetImageObjects [b].GetComponent<MeshRenderer> ()));
        }

    }
  
    // Update is called once per frame
    void LateUpdate () {

        for (int i = 0; i < newList.Count; i++) {
            if (          ) {

Please help me i have a deadline verysoon
thanks

Sorry, too much code to consider so I can just guide you with a possible solution depending on if isVisible is a state of the property ‘meshrener’:

for (int i = 0; i < newList.count; i++)
{
   if (newList[i].meshrener.isVisible)
   {
     transform.SetParent(targetImageObjects[i].transform.parent.gameObject.transform, false);
   }
}
1 Like

For the most part, you can treat a List like you treat an array. However, watch out for this trick when initializing the List:

Also, List implements the IEnumerable interface, which means you can use the foreach loop construct:

foreach (var item in newList)
{
   if (item.meshrener.isVisible)
   {  }
}

The advantage is you no longer need to use an indexer to access the correct item and it also protects you from making changes to the list you are enumerating over (which can be dangerous if you are not very careful). The disadvantage is that you no longer know at what index you are at, so it becomes difficult to access corresponding items in other lists. Thus it would be impossible to implement your specific algorithm without some additional changes.

1 Like