- Home /
Unity editor freezes but scripts continue to run
I am writing some basic networking code for a game, still just trying the basics. for some reason, my unity editor freezes and won't respond to anything while the code keeps executing as evident by the visual studio pausing in the debugger, it keeps changing and iterating. I assume I have a loop which keeps running and doesn't return control to unity.
here's the main loop of the server code:
NetIncomingMessage msg;
void FixedUpdate()
{
/* NETWORKING BLOCK */
msg = server.ReadMessage();
if (msg != null)
{
switch (msg.MessageType)
{
case NetIncomingMessageType.Data:
{
//HANDLE DATA
print("server received message " + msg.PeekString());
string message = msg.PeekString();
UserConnection sender = GetUser(msg.SenderConnection);
Rigidbody rb = sender.playerObject.GetComponent<Rigidbody>();
if (message.Contains("input"))
{
int index = message.IndexOf("input") + 5; //index of character after input
while (index < message.Length)
{
if (message[index] == 'm') //if movement
{
print("started movement");
index++; //move to field after m
switch (message[index])
{
case '0': //forward
{
ApplyVelocity(rb, Vector3.forward);
break;
}
case '1':
{
ApplyVelocity(rb, Vector3.back);
break;
}
case '2':
{
ApplyVelocity(rb, Vector3.left);
break;
}
case '3':
{
ApplyVelocity(rb, Vector3.right);
break;
}
case '4':
{
ApplyVelocity(rb, Vector3.up);
break;
}
case '5':
{
ApplyVelocity(rb, Vector3.down);
break;
}
case '6':
{
ApplyBrake(rb);
break;
}
}
index++;
print("finished movement");
}
else if (message[index] == 'r') //if passed delta rotation
{
print("inside rotation");
index++; //move to field after r
int startIndex = index;
int endIndex = startIndex + 1;
while (endIndex < message.Length && message[endIndex] != ',')
{
endIndex++;
}
float x;
float.TryParse(message.Substring(startIndex, endIndex - startIndex), out x);
startIndex = endIndex + 1;
endIndex += 2;
while (endIndex < message.Length && message[endIndex] != ',')
{
endIndex++;
}
float y;
float.TryParse(message.Substring(startIndex, endIndex - startIndex), out y);
startIndex = endIndex + 1;
endIndex += 2;
while (endIndex < message.Length && message[endIndex] != ',')
{
endIndex++;
}
float z;
float.TryParse(message.Substring(startIndex, endIndex - startIndex), out z);
startIndex = endIndex + 1;
endIndex += 2;
while (endIndex < message.Length && message[endIndex] != ',' && message[endIndex] != 'm' && message[endIndex] != 'r') //check that doesn't enter next field
{
endIndex++;
}
float w;
float.TryParse(message.Substring(startIndex, endIndex - startIndex), out w);
sender.playerObject.transform.rotation *= new Quaternion(x, y, z, w);
print("finished rotation");
}
}
}
else if (message.Contains("pos"))
{
//read position and rotation and sets it up for the first time
print("started handling first pos");
if (sender.IsValid() && sender.canSendPos)
{
//parse vector3
int startIndex = 3;
int index = 4;
while (index < message.Length && message[index] != ',')
{
index++;
}
float x;
float.TryParse(message.Substring(startIndex, index - startIndex), out x);
float y;
startIndex = index + 1;
index = startIndex + 1;
while (index < message.Length && message[index] != ',')
{
index++;
}
float.TryParse(message.Substring(startIndex, index - startIndex), out y);
float z;
startIndex = index + 1;
index = startIndex + 1;
while (index < message.Length && message[index] != ',')
{
index++;
}
float.TryParse(message.Substring(startIndex, index - startIndex), out z);
sender.playerObject.transform.position = new Vector3(x, y, z);
//parse quaternion
float qX;
startIndex = index + 1;
index = startIndex + 1;
while (index < message.Length && message[index] != ',')
{
index++;
}
float.TryParse(message.Substring(startIndex, index - startIndex), out qX);
float qY;
startIndex = index + 1;
index = startIndex + 1;
while (index < message.Length && message[index] != ',')
{
index++;
}
float.TryParse(message.Substring(startIndex, index - startIndex), out qY);
float qZ;
startIndex = index + 1;
index = startIndex + 1;
while (index < message.Length && message[index] != ',')
{
index++;
}
float.TryParse(message.Substring(startIndex, index - startIndex), out qZ);
float qW;
startIndex = index + 1;
index = startIndex + 1;
while (index < message.Length && message[index] != ',')
{
index++;
}
float.TryParse(message.Substring(startIndex, index - startIndex), out qW);
sender.playerObject.transform.rotation = new Quaternion(qX, qY, qZ, qW);
sender.canSendPos = false;
print("finished handling first pos");
}
}
break;
}
case NetIncomingMessageType.StatusChanged:
{
switch (msg.SenderConnection.Status)
{
case NetConnectionStatus.Connected:
{
connectedUsers.Add(new UserConnection(msg.SenderConnection, (GameObject) Instantiate(NetworkedPlayerPrefab, Vector3.zero, Quaternion.Euler(0, 0, 0)))); //spawns the player at 0,0,0 and rotation 0,0,0
print("user conncted to server");
SendString("acceptedConnection");
break;
}
case NetConnectionStatus.Disconnected:
{
print("user disconnected from server");
//removes the user from the connection list
NetConnection disconnectingConnection = msg.SenderConnection;
int indexToRemove = -1;
for (int i = 0; i < connectedUsers.Count; i++)
{
if (connectedUsers[i].clientConnection == disconnectingConnection)
{
indexToRemove = i;
}
if (indexToRemove != -1)
{
connectedUsers.RemoveAt(indexToRemove);
}
}
break;
}
}
break;
}
default:
print("unhandled error" + msg.MessageType + ", " + msg);
break;
}
server.Recycle(msg);
}
}
Indeed, that's the case. The first while will only increase index, if it bumps into an "m" or an "r" (line 22, I think?). Otherwise, index will never increase and the loop never ends.
Unity tends to freeze in infinite loops. Check your while ending conditions are met
Your answer
Follow this Question
Related Questions
Importing from Maya 2 Answers
New User, New Project 0 Answers
unity freezes repeatedly 1 Answer