Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dainiusss · Feb 28, 2015 at 12:25 PM · c#gameobjectlistfor-loopforeach

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;
     }



Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siaran · Feb 28, 2015 at 12:37 PM 1
Share

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.

avatar image Dainiusss · Feb 28, 2015 at 02:37 PM 0
Share

There are 3 different tiles (for now), the adding tile is calculated randomly.

avatar image Owen-Reynolds · Feb 28, 2015 at 06:35 PM 1
Share

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.

avatar image Dainiusss · Feb 28, 2015 at 07:23 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dainiusss · Feb 28, 2015 at 02:49 PM 0
Share

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...

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dainiusss · Feb 28, 2015 at 05:54 PM 0
Share

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.

avatar image bartm4n · Feb 28, 2015 at 06:15 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How would I find the name of all game objects in a c# List 1 Answer

C# List foreach problem 1 Answer

A couple of questions about my GameManager 1 Answer

Foreach Statement Random Sequence/Order? 3 Answers

foreach a array inside a generic list 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges