[SOLVED] Why is this block of code giving me a "NullReferenceReference Exception" error?

Line 16-20 are giving me the errors.

                public enum TargetingType
                {
                        LastPoint,
                        ClosestPoint,
                        FarthestPoint,
                        LowestPoint,
                        HighestPoint,
          
                }
                public TargetingType targetingType;
                public delegate Vector2 TargetingTypeDelegate (List<Vector2> points,Vector2 start);
                public static List<TargetingTypeDelegate> TargetingTypeDelegates;

                protected virtual void Awake ()
                {
                        TargetingTypeDelegates.Add (Extensions.GetLastPoint);
                        TargetingTypeDelegates.Add (Extensions.GetClosestPoint);
                        TargetingTypeDelegates.Add (Extensions.GetFarthestPoint);
                        TargetingTypeDelegates.Add (Extensions.GetLowestPoint);
                        TargetingTypeDelegates.Add (Extensions.GetHighestPoint);
                }

Because the list to which you are adding the deletages is null. You have to create it before you use it, e.g. like that:

public static List<TargetingTypeDelegate> TargetingTypeDelegates = new List<TargetingTypeDelegate> ();

I don’t understand why you are using a list of delegates, because delegates can already deal with an arbitrary number of methods.

2 Likes

Oh can’t believe I missed that!
Thanks.

+1 for Dantus. No need to use a List of delegates.

It’s not the full code. I used a list of delegates because I also have an enum I use to control which delegate will be run.