- Home /
Unity freezes after invoking method in loop of the same method (C#)
First of all, my apologies for the vaguely formulated title. After hours of trying to understand and solve this problems (the last one in vain), I'm not capable of formulating the problem in one small sentence.
Context
I've built a system in which certain methods (we call them 'Actions') can be visually scripted when certain conditions are met, e.g. MoveToPosition. Since actions should be able to have sequential actions (triggered after the end of an action), the Action instances have a property NextActions, which is a List of IDs. Every time an action has ended, the event OnActionEnd is raised, which leads to the following method.
public void OnActionEnd()
{
foreach(string actionID in nextActions)
{
ActionManager.InvokeAction(actionID);
}
}
public class ScheduleAction
{
// ID of the Action
public string actionID;
// GameObject that holds the Component in which the
// method that needs to be invoked resides
public GameObject targetObject;
// The name of the method that needs to be invoked
public string method;
// The list of parameters that will be passed to the
// method that will be invoked
public List<object> parameters;
// List of Action IDs that need to be triggered after
// the method of this action reached his end
public List<string> nextActionIDs;
// Flag indicating if the Action should be triggered or not
// (could be false for debugging purposes)
public bool activated;
public void InvokeAction()
{
if (activated && !string.IsNullOrEmpty(method))
{
string componentName = method.Substring(0, method.IndexOf("/"));
string methodName = method.Substring((method.IndexOf("/") + 1));
if (!string.IsNullOrEmpty(componentName) && !string.IsNullOrEmpty(methodName))
{
targetObject.InvokeInComponent(componentName, methodName, parameters.ToArray());
}
}
}
}
Problem
Now for the problem... Unity freezes and ultimately crashes during this method. Why? Because when OnActionEnd is triggered after an action, it begins the foreach
loop with invoking the first action in the list NextActions. Since that NextAction could be just one line of code (e.g. SetColor), it will immediately trigger OnActionEnd, causing the loop to.. loop. Forever, because the first call to OnActionEnd has never finished and is at that time still at the ActionManager.InvokeAction(actionID);
line.
Unfortunately, I don't know the details of what's exactly going wrong nor have I got a solution for this. I've got a feeling I'm totally overlooking this, but since I'm the only programmer on the team I don't have someone to brainstorm with.
Tried solutions
I've tried a multi-threaded approach, which seemed to work at first, until I started invoking methods involving Unity components.. (you can't invoke UnityEngine methods outside the main thread).
I've tried using delegates with the event system but since the ScheduleAction script needs to be serializable (thus can't inherit from MonoBehaviour), I can't use the
OnEnable()
andOnDisable()
methods to subscribe theInvokeAction()
to events.I've tried using a regular for-loop, to no avail.
did you try using a simple for ins$$anonymous$$d of for each? unity crashes when there is an infinite loop that happened to me as well and i was mad because there is no autosave :P
@ExtinctSpecie, thank you for your suggestion. As a matter of fact, I've actually tried that as well, to no avail. I've included it in the tried solutions section.
To me it sounds like if you have 2 actions that both have eachother as "follow up actions" and you execute either one of them, it will already create the kind of endless recursion you are dealing with.
Even if there were actions without follow ups, all it takes is 2 actions that end up executing eachother and you're stuck.
I guess first you at least have figure out if this is the problem. If it is, that needs to be solved on paper, design-wise before writing more code.
If you have actions ColorizeToGreen and ColorizeToRed and they both lead into executing eachother, what should the result be? A red object, a green object or an object that changes color every x seconds?
Your answer
