Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Solu9 · Mar 27, 2016 at 08:10 PM · listfunctionforeachvoid

How to run a "foreach" through a list of voids

Hey folks

I have tried to figure out how to store several voids in a list and activating one of them depending on which level the player has reached.

I have searched how to setup a list of functions and it mentioned the "delegate void". To be honest I have no idea what that is but it seem to work. These are the variables I have:

     delegate void levels();
     public GameObject[] levelLayout;
     public GameObject currentLevelLayout;
 
     public int currentLevel;


First I fill the list with the level setup functions (this is run from the start function). The "LevelSetup_001" and 002 are functions:

     void CreateLevelList()
     {
         levelList.Add(LevelSetup_001);
         levelList.Add(LevelSetup_002);
     }

Then when a new level is to be instantiated I need to find the corresponding level setup function. This is also where my problem lie. What should I write in the "foreach" to go through the function list?:

    public void CreateLevel()
    {
     int tempInt = -1;

     foreach (????  ???? in levelList)
     {
         tempInt += 1;
         if (tempInt == currentLevel)
         {
             levelList[tempInt]();
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ahndrakhul · Mar 27, 2016 at 10:03 PM

Your list is a list of delegates. Delegates are basically just references to methods (functions). when you do:

 delegate void levels();

you are declaring a delegate named "levels" that can be assigned references to methods that have the same signature. In this case, methods that don't return anything (void) and don't accept any parameters. All of your levelSetup methods must have this signature, so your "LevelSetup_001" must look like "void LevelSetup_001(){}" C# actually has a predefined delegate called an "Action" that is the same as your "levels" delegate.

Anyway, You would use the type you used when you declared the list, so probably "levels"

 foreach (levels level in levelList)
 {
      tempInt += 1;
      if (tempInt == currentLevel)
      {
          level();
      }
 }

However, it doesn't look like you actually need a foreach statement here. You could replace your CreateLevel() method with:

 public void CreateLevel()
 {
     levelList[currentLevel]();
 }

I don't see how you assign a value to "currentLevel" and I don't know how much you know about list indexes, but you have to be careful. When you do this:

 void CreateLevelList()
 {
     levelList.Add(LevelSetup_001);
     levelList.Add(LevelSetup_002);
 }

LevelSetup_001 will be at levelList[0] and levelList_002 will be at levelList[1]. So, If you are trying to setup level 1 and your currentLevel variable equals 1, you will actually call LevelSetup_002.

I think I would use a dictionary of Action delegates here instead of a list, or even a switch statement if you aren't going to have a lot of levels.

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 Solu9 · Mar 28, 2016 at 03:22 PM 0
Share

Alright. I understand most of what you are saying. I should note I have this along side with the other variables: List levelList = new List();

I'm still not sure how to use it properly.

Anyway. Your levelList[currentLevel](); is awesome. Don't know why I didn't think of that. But I seem to have found another problem which might just be the same problem as before.

When I want to execute the CreateLevel function from another script it hands me an error: "Object reference not set to an instance of an object". I can't figure out why it gives me this error.

For simplecity: I have two scripts. ControllerStats and LevelSetup. The ControllerStats has a variable which stores the LevelSetup as component and vice versa. From ControllerStats I run an update checking if "q" has been clicked. If yes: _levelSetup.CreateLevel(); (where _levelSetup is the stored component.

This is where it will give me the error. I have tried a debug message at the very beginning of the CreateLevel in the LevelSetup script, but it does not display.

avatar image
0

Answer by Solu9 · Mar 28, 2016 at 05:53 PM

Oh my god... After running through my entire ControllerStats script I found the Start() function, which was spelled with... a lower case "s"...

Problem solved. The levels load as they should now. Thank you Ahndrakhul for pointing out the levelList[currentLevel](); approach.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Total war Style Movement System with Right Click 0 Answers

Get object to check list for number of specific items 0 Answers

void functions not showing in OnClick() 0 Answers

C# function with multiple float arguments not working 1 Answer

"InvalidOperationException Collection was modified" in a foreach loop 0 Answers


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