- Home /
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 [i].m_MapTile.SetActive (IsActive);
}
public List<Tile> m_BlockTiles = new List<Tile>();
}
//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<MapBlockGenerate> m_BlockStorage = new List<MapBlockGenerate>();
//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;
}
Are you sure you are adding different objects to your list? From what you are describing, it sounds like you keep adding the same object to your list.
There are 3 different tiles (for now), the adding tile is calculated randomly.
sairan: yes, the adding is sort of nonsense. m_tile
is being added every time, but never changes. $$anonymous$$eanwhile, the "added" tiles are merely made children of something named "$$anonymous$$apTiles." Looks like the OP is mixing up a real list with a unity-style gameObject+kids list.
Then m_AddTile
is a member function of m_tile
, so you assume it's doing something with it, but it isn't.
The code making the list seems to be a car crash.
I thought that I can overload an object and put it into a List, like I was doing with stl vectors in C++ (I am new to C#, so I don't understand List properly yet). The code looks bad because it's hard for me to compress a lot of lines into small space... This List became a real headache to me. Thanks for help.
Answer by fafase · Feb 28, 2015 at 12:37 PM
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);
}
I still don't get it. In my project, there is a class Tile, which has one GameObject with it. In other class "$$anonymous$$apBlockGenerate", there is a List of Tiles, holding 100 Tiles (thats a block of tiles). Third class is just a main class, which has second List, which, obviously, holds blocks. I need to disable a specific block at specific time, so thats why I am doint it like this. I can't do a loop like this: foreach(GameObject obj in list), because I don't hold GameObjects in list, I hold Tiles, so it should be like that: foreach(Tile tile in list). And I don't understand, what is wrong with my loop and why should I use Extensions? All tutorials showed for or foreach loops like I am using...
Answer by bartm4n · Feb 28, 2015 at 04:04 PM
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: https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
I hope that this helps.
I have added all elements, with adding proccess everything is clear- all the elements I need are in the List. I just skipped this part of code im my question, because there are a lot of lines. Also, thanks for documentation.
Then there has to be a logic error in the code not displayed. The built-in Lists implement IEnumerable and thus support iteration in all of the ways that you have described.
I suggest that you place a breakpoint at the start of your loops and inspect the contents of your List. If that sounds unfamiliar to you, you should look at debugging tutorials for the IDE that you use.
I don't use $$anonymous$$onoDevelop and I don't know if you do, but this is a good start: http://docs.unity3d.com/430/Documentation/$$anonymous$$anual/Debugger.html