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.
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?
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;
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:
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.
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.