For loop does not loop C#

Hello, everyone. I made a List<> filled with objects of class, which has a gameObject. I need to make these gameObjects in a List inactive, so I made a function with a for and foreach loop. Unfortunately, both of them don’t work for me and I can’t find out why. They only switches off my first object of a list, but does not change others. Here is some code:

public class MapBlockGenerate
{ //I guess that mistake is with how I am adding objects to a List, see: mL_BlockTiles.Add(m_tile);
	public void m_GenerateBlock(Vector3 StartingPos) //Adding to list 
{
	m_BlockStartPos = StartingPos;
	Starting ();
	for (int i = 0; i < 100; i++)
	{
		int tile_type = Random.Range(0, m_different_tiles);
		if (i == 0) 
		{
			m_tile.m_AddTile (StartingPos, tile_type);
			mL_BlockTiles.Add(m_tile);
		}
		else if (i % 10 == 0)
		{
			m_tile.m_AddTile(new Vector3(StartingPos.x,
			                         mL_BlockTiles[0].m_MapTile.transform.position.y - mL_BlockTiles[0].m_MapTile.renderer.bounds.size.y, 0.0f), tile_type);
			mL_BlockTiles.Add(m_tile);
		}
		else 
		{
			m_tile.m_AddTile(new Vector3(mL_BlockTiles[0].m_MapTile.transform.position.x + mL_BlockTiles[0].m_MapTile.renderer.bounds.size.x,
			                         mL_BlockTiles[0].m_MapTile.transform.position.y, 0.0f), tile_type);
			mL_BlockTiles.Add(m_tile);
		}
	}
}
public void m_BlockActive(bool IsActive)
	{
		//foreach(Tile MapTile in BlockTiles) MapTile.m_MapTile.SetActive(IsActive);
		for (int i = 0; i < m_BlockTiles.Count; i++) m_BlockTiles *.m_MapTile.SetActive (IsActive);*
  • }*

public List m_BlockTiles = new List();
}
//In other class I do the action :
void Start() {
Block.m_GenerateBlock(new Vector3(-40.0f, 0.0f, 0.0f));
mL_BlockStorage.Add (Block);
m_BlockStorage [0].m_BlockActive (false);
}
public List m_BlockStorage = new List();
//Less important code down here:
//Tile class has a gameObject, called m_MapTile, which I want to access:

public GameObject m_MapTile;

public void m_AddTile(Vector3 pos, int type)

  • {*
  •  m_MapTile = GameObject.Instantiate (Storage.Tiles[type], pos, Quaternion.Euler (0.0f, 0.0f, 0.0f)) as GameObject;*
    
  •  m_MapTile.transform.parent = GameObject.Find ("MapTiles").transform;*
    
  • }*

m_BlockStorage [0].m_BlockActive (false);

this line is kinda weird, you would expect some kinda of list call while you are calling on a particular item. I would recommend an extension method:

public static class Extensions{
    public static ActivateList(this List<GameObject>list, bool isActive){
          foreach(GameObject obj in list){
              obj.SetActive(isActive);
          }
    }
}

And in your script you use it:

List<GameObject> m_BlockStorage = new List<GameObject>();

void Start()
{
  m_BlockStorage.ActivateList(false);
}

Either you omitted the section of code that actually adds tiles to the list, or you aren’t doing it at all. Assuming that it was not omitted, you would need to use the List.Add(t) method.

MSDN documentation of List:

I hope that this helps.