- Home /
Question was wrong, simple mistake, no answers submitted before problem found...comments reflect this.
Unity crashing with BroadcastMessage (possibly not thread safe?)
I have a random number of nodes which are connected via a structure I call the transfer manager. It allows items to be transferred from one node to another. As far as my testing has gone, everything seemed to work perfectly until I implemented BroadcastMessage to call a function on every transfer manager, followed by a call to every node.
very simply this starts the call:
if(GUI.Button(new Rect(Screen.width-101,0, 100, 100), "End Turn"))
{
allParent.BroadcastMessage("ResolveTransfers");
}
followed by this script running on all transferManagers simultaneously.
void ResolveTransfers()
{
if (tempOne.fleetShips > 0 && tempTwo.fleetShips > 0 && tempOne != tempTwo)
{
List<Fleet> tempList=new List<Fleet>();
tempList.Add(tempOne);
tempList.Add(tempTwo);
Fleet.Battle(tempList);
}
if (tempOne != null)
{
two.AddFleet(tempOne);
}
if (tempTwo != null)
{
two.AddFleet(tempTwo);
}
tracker++;
if (tracker== count)
{
print(count);
GUIWindows.instance.allParent.BroadcastMessage("OnEndOfTurn");
tracker = 0;
}
}
The part that concerns me is the last part, Its designed so that the message is sent only once: when the last running instance of the script (count is static and incremented in start, so it should be the number of instances) hits that point, but I'm starting to think that it might not be thread safe and is causing the crash? The next broadcastMessage is only calling an empty function at the moment, so its not the problem.
I'm really at a loss here and I've wasted 6 hours today trying to get this working. I would really appreciate any help with this.
Its tough to say what you are experiencing, but make sure that by broadcasting GUIWindows.instance.allParent.Broadcast$$anonymous$$essage("OnEndOfTurn");
your code doesn't call: Broadcast$$anonymous$$essage("ResolveTransfers"); again. This would throw unity into a infinite loop, you would get no error messages, it would just lock up, sit there for a few seconds and quit.
I actually just found the problem, I overloaded the equality operator and then inside the operator checked if it was == null, causing infinite recursion. Thanks for the time though, closing question.
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Procedural generated mesh problem 1 Answer
adding items to my list 1 Answer