I am using Unity3D. Here is my Asynchronous method:
private void Receive(IAsyncResult ar)
{
try
{
IPEndPoint ipEndPoint = null;
byte[] data = udpClient.EndReceive(ar, ref ipEndPoint);
string receivedMessage = Encoding.UTF8.GetString(data);
JsonData json = JsonConvert.DeserializeObject<JsonData>(receivedMessage);
string prefix = json.header.Substring(0, 2);
Debug.Log("UDP World: " + receivedMessage);
if (prefix != "3x")
{
Debug.Log("Unknown packet: " + receivedMessage + "\n");
}
else
{
string header = json.header.Substring(2);
int conId = json.connectionId;
switch (header)
{
default:
Debug.Log("Unknown packet: " + receivedMessage + "\n");
break;
case "001":
Debug.Log("Data received: " + receivedMessage + "\n");
break;
case "002":
CharacterPosition position = new CharacterPosition();
position.x = float.Parse(json.data["position.x"].ToString());
position.y = float.Parse(json.data["position.y"].ToString());
position.z = float.Parse(json.data["position.z"].ToString());
Character.updateCharacterPosition(position, Convert.ToInt32(json.data["characterId"].ToString()), conId);
break;
case "003":
ClientWorldServer.character.updateCharacterRotation(json.data, conId);
break;
}
}
} catch (SocketException e)
{
Debug.Log("UDP Socket Exception: " + e);
}
udpClient.BeginReceive(Receive, null);
}
Here is the function which is not working corectly when called from Receive()
:
public static void updateCharacterPosition(CharacterPosition position, int characterId, int cnnId)
{
if (Character.SceneLoaded != true)
return;
Debug.Log("Position 2: X: " + position.x + " Y: " + position.y + " Z: " + position.z);
Debug.Log("GameObject FIND: " + GameObject.Find("CharactersOnline").name);
if (cnnId != ClientWorldServer.connectionId)
{
GameObject startPoint = GameObject.Find("CharactersOnline/" + characterId.ToString());
GameObject endPoint = new GameObject();
endPoint.transform.position = new Vector3(position.x, position.y, position.z);
GameObject.Find("CharactersOnline/" + characterId.ToString()).transform.position = Vector3.Lerp(startPoint.transform.position, endPoint.transform.position, Time.deltaTime);
//Updating Clients ram of the character's position
Character.characterDetails[int.Parse(characterId.ToString())].characterPosition.x = position.x;
Character.characterDetails[int.Parse(characterId.ToString())].characterPosition.y = position.y;
Character.characterDetails[int.Parse(characterId.ToString())].characterPosition.z = position.z;
//Destroy(endPoint);
}
}
All I see in Unity's console is the output from:
Debug.Log("Position 2: X: " + position.x + " Y: " + position.y + " Z: " + position.z + " Character: " + characterId + " CnnID: " + cnnId);
With the Debug.Log shown above I can see that i have all the information needed. Everything in the Debug.Log is there. The next line:
Debug.Log("GameObject FIND: " + GameObject.Find("CharactersOnline").name);
seems to be the place where everything brakes. I do not receive any output from that line neither error whatsoever. It just looks like it stays there not giving any output or error.
When updateCharacterPosition
is called from another method which is not Asynchronous everything inside updateCharacterPosition
is working as indented.
Any idea why this strange behavior is occurring. How can I fix that so i can call updateCharacterPosition
from Receive
which is Async function?