Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity action example

public static event Action<string> OnGameOver;
public void TakeDamage(float damage)
{
    health -= damage;
    if(health < 0)
    {
        OnGameOver?.Invoke("The game is over");
    }
}

// can have multiple paramethers, always return null (if you want that it 
// retur something you have to create your own event

public static event Action<string> OnGameOver;
public static event Action<float, bool> OnPlayerHurt;
Comment

Unity action

//You declare that you have a list of actions:

List<Action> list;
//Do not forget to initialize it:

list = new List<Action>();
//Then, you create an action from the function using a lambda expression:

Action action = () => Func1(1);
//And you add the action to the list:

list.Add(action);

//You can, of course, inline the lambda expression:
list.Add(() => Func1(1));

//Afterwards you can call the actions, for example, in a loop:
foreach (var action in list)
{
    action.Invoke();
}
//Actually, you do not need to use Invoke:
foreach (var action in list)
{
    action();
}
//Ah, yes, that works too:
list[position]();
Comment

PREVIOUS NEXT
Code Example
Csharp :: difference between alpha and beta testing 
Csharp :: c# get last 3 characters of string 
Csharp :: how to add item in list at first position c# 
Csharp :: c# if int is in range 
Csharp :: unity string lowercase 
Csharp :: csharp datagridview filter column 
Csharp :: remove all non alphabetic characters from string c# 
Csharp :: getmousebuttondown unity 
Csharp :: html.beginform 
Csharp :: rock paper scissors c# 
Csharp :: c# serial port 
Csharp :: change image of button c# 
Csharp :: c# isarray 
Csharp :: c# list declaration 
Csharp :: change color of object unity 
Csharp :: c# insert spaces before capital letters 
Csharp :: c# not 
Csharp :: calculate how much memory an object take c# 
Csharp :: how to select time and date in datetimepicker in c# 
Csharp :: split string c# 
Csharp :: arcane 
Csharp :: .net core web app get dll name 
Csharp :: c# change language version to 9.0 
Csharp :: c# kill one excel process 
Csharp :: c# regex replace all line breaks 
Csharp :: c# picturebox transparente 
Csharp :: from string 
Csharp :: c# int 
Csharp :: what is void onmousedown() 
Csharp :: C# fileinfo creation date 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =