Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `Action` with Unity C#?

Tags:

c#

unity3d

I want to use Times function in a Unity, by following this site I use this script.

public static class IntExtensions
{
    public static void Times(this int i, Action<int> func)
    {
        for(int j = 0; j < i; j++)
        {
            func(j);
        }
    }
}

But it causes error:

Assets/Resources/Scripts/Board.cs(27,40): error CS0246: The type or namespace name Action 1 could not be found. Are you missing a using directive or an assembly reference?

It looks like there is no Action in Unity C#.

Is there way to use the function in Unity? And where should I put the file if I want to use throughout my Unity project?

like image 603
ironsand Avatar asked Jun 01 '14 01:06

ironsand


1 Answers

The Action<T> delegate type is defined in the System namespace - make sure you have using System; as a directive at the top of your CS file.

like image 87
Preston Guillot Avatar answered Sep 27 '22 18:09

Preston Guillot