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
1
Question by Hullu · Sep 19, 2015 at 05:46 PM · c#listmethods

How to make list of methods (example Sims)

How can i list methods?

 public List<???> Actions = new List<???>();

and then i would call the top one in the list every time previous method is completed or something like that.

Or is there a better way to do this?

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

1 Reply

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

Answer by RudyTheDev · Sep 19, 2015 at 08:55 PM

It depends how complex and OOP you want to go with this. In the simplest case:

 using System;
 using System.Collections.Generic;
 using UnityEngine;

 public class ActionClass : MonoBehaviour
 {
     private List<Action> actions;
 
     private void Start()
     {
         actions = new List<Action>();
 
         actions.Add(Action1);
         actions.Add(Action2);
         actions.Add(Action3);
     }
 
     private void Action1() { }
     private void Action2() { }
     private void Action3() { }
 }

Something like Sims is sure to do it much more involved. An OOP base would look something in the lines of:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class AbilityClass : MonoBehaviour
 {
     private List<Ability> actions;
 
     private void Start()
     {
         actions = new List<Ability>();
 
         actions.Add(new WatchTV());
         actions.Add(new TakeBath());
         actions.Add(new MakeFood(3));
     }
 }
 
 public abstract class Ability
 {
     public abstract void Do();
 }
 
 public class WatchTV : Ability
 {
     public override void Do()
     {
         Debug.Log("Watching TV");
     }
 }
 
 public class TakeBath : Ability
 {
     public override void Do()
     {
         Debug.Log("Taking a bath");
     }
 }
 
 public class MakeFood : Ability
 {
     public int servings;
 
     public MakeFood(int servings)
     {
         this.servings = servings;
     }
 
     public override void Do()
     {
         Debug.Log("Making food");
     }
 }
Comment
Add comment · Show 5 · 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 Hullu · Sep 20, 2015 at 06:18 AM 0
Share

I will try to work from this, Thanks!

avatar image lucspinto · Mar 29, 2018 at 02:24 PM 0
Share

And how can I make Abilities like $$anonymous$$akeFood play an animation and sound?

avatar image sharangsharma09 · Dec 14, 2018 at 11:14 AM 0
Share

@$$anonymous$$TheDev @Hullu thanks for your answer. How do you invoke all the methods in the list?

 foreach (Ability action in actions) { action(); }



This doesn't seem to work :(

avatar image Hellium sharangsharma09 · Dec 14, 2018 at 11:28 AM 1
Share
 foreach (Ability action in actions) { action.Do); }
avatar image Mopto · Jan 25, 2021 at 02:40 PM 0
Share

How can I sort this list For example, first in list will be $$anonymous$$akeFood than others?

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers

Is there anyway to make a list of prewritten variables? (C#) 2 Answers

Arbitrary line in one method prevents another for working? 1 Answer

Insert string into empty list at a specific index 0 Answers

Referencing uninstantiated GameObject within a class? 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