List<> of Delegates not accessible

Hey guys,
I’ve got a game that has to iterate through several types of GameObjects to check if a level has been won or not. In order to avoid performance problems I thought about splitting those queries up and call each one only once per second at different times (eg after 0.45 and 0.9 seconds for two queries). To make this concept of currently two queries expandable I wanted to use delegates in List. Here’s my approach:

delegate void myDelegate();
public class timedFunction
{
	public myDelegate Delegate;
	public bool executed = false;
}
List<timedFunction> timedCalls;

Creating new timedFunctions and adding them to the timedCalls list works without an error, but trying to look at the elements of my List right after having added them in MonoDevelop results in the message:

Naturally my code iterating through the List also returns an error, in this case:

I believe this is only an aftereffect of the first error.

Any ideas on what I’m doing wrong here?

Thanks!

The first error seems only to be a problem in MonoDevelop? In this case (c# - Unity, weird debugging - Stack Overflow) lets skip to the other problem, how do you initialize your List, can we see a little bit more code?

You must first create the list before you can add objects to it

List<timedFunction> timedCalls = new List<timedFunction>();

Thanks so far.
Here’s a collection of the relevant code:

Declarations:

public delegate void myDelegate();
	public class timedFunction
	{
		public myDelegate Delegate;
		public bool executed = false;
	}
	List<timedFunction>timedCalls;

Initialisation (this works without problems)

void Start () 
	{	
		timedCalls = new List<timedFunction>();
		timedFunction tF1 = new timedFunction();
		tF1.Delegate = updateCarVars;
		tF1.executed = false;
		timedCalls.Add (tF1);
		timedFunction tF2 = new timedFunction();
		tF1.Delegate = updateSpawnVars;
		tF1.executed = false;
		timedCalls.Add (tF2);	
	}

Usage:

if(timedCalls[timedNo - 1].executed == false)
				{
					timedCalls[timedNo - 1].Delegate();
					timedCalls[timedNo - 1].executed = true;
					timedNo++;
				}

Of course I checked wether I really try to access s.th. that’s out of range, which is defenitely not the case!

Thank you!

Look’s like what you trying to do is the same concept of Queue. Am i wrong?
What is the code before this:

if(timedCalls[timedNo - 1].executed == false)

I wonder if your variable starts with “0”. correct me if i’m wrong, but then your if would get an erro, right!?

The delegate is a reference type, check whether it is null before invoking the delegate.

Important: I changed the code slightly and changed the ID handling (timedNo - 1). This was NOT the reason for the error. I changed it because it is easier to read. So here’s the complete code, called from update:

void updateTimedVariables()
{
	if(elapsedTime > (1f / (float)timedCalls.Count * (float)timedNo + 1f) - 0.1f)
	{
		if(timedNo < timedCalls.Count)
		{
			if(timedCalls[timedNo ].executed == false)
			{
				timedCalls[timedNo].Delegate();
				timedCalls[timedNo].executed = true;
				timedNo++;
			}
		}
	}
}

An explanation:
Elapsed time gets resetted as soon as it exceeds 1 second, because I want all functions to be called once a second.
If the elapsed time is bigger than 1 second split by the amount of functions that have to be called multiplied with the timedNo (= the ID of the function + 1, because I don’t want it to multiply with 0) AND it hasn’t been called yet (that’s what the bool is for) I try to call the Delegate and change the bool.
One more thing: The exact line of the error, if you believe the Unity Console, is the one with executed = true;
I reset timedNo and the executed variable at some other point.

Cheers

Marrrk, when setting a breakpoint in Start(), the delegate of tF1 isn’t null. Still you kind of provided the solution. Instead of adding a delegate and bool to tF2, I do it again with 1 (didn’t change the name when copy / pasting) , wich then led to the error.
If Mono wouldn’t have had the List<>-Error, I would have found that out right away =/
Thanks for all your help anyway.