- Home /
Why this code does not execute?
I have this code inside loop and it stops executing when it reaches to point where it logs "Started player handling", what is wrong?
using UnityEngine; using System.Collections; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading;
public class PlayerUpdaterCS : MonoBehaviour {
public GameObject connectivity; public GameObject gamer; public string[] existingplayers; private Transform transformer; public bool connected; Thread receiveThread;
// Use this for initialization void Start () { init(); }
private void init() { receiveThread = new Thread(new ThreadStart(PlayerUpdate)); receiveThread.IsBackground = true; }
private void PlayerUpdate() { while(true) { Debug.Log("did i get here?"); string message = Messaging.ReceiveMessage(); HandleMessage(message); } }
private void HandleMessage(string message){ string[] osat = message.Split('>'); string playeridS = osat[0].Replace("ID:", ""); int playerid = int.Parse(playeridS); string playername = osat[1].Replace("N:", "");
string playerxyz = osat[2];
//muuttaa xyz arvot
string[] xyz = playerxyz.Split('Y');
float x = float.Parse(xyz[0].Replace("X:", ""));
string[] yz = xyz[1].Split('Z');
float y = float.Parse(yz[0].Replace(":", ""));
float z = float.Parse(yz[1].Replace(":", ""));
Vector3 sijainti;
sijainti.x = x;
sijainti.y = y;
sijainti.z = z;
Debug.Log("Finished with message handling");
HandlePlayers(sijainti, playerid, playername);
}
private void HandlePlayers(Vector3 sijainti, int playerid, string playername) { Debug.Log("Started player handling"); GameObject otherplayer; if(existingplayers[playerid] == playername) { Debug.Log("did i get here?5"); otherplayer = GameObject.FindWithTag ("OtherPlayer"+playerid); otherplayer.transform.position = sijainti; Debug.Log("did i get here?6"); } else { Debug.Log("did i get here?3"); existingplayers[playerid] = playername; otherplayer = Instantiate(gamer, sijainti, Quaternion.identity) as GameObject; otherplayer.tag = "OtherPlayer"+playerid; Debug.Log("did i get here?4"); } }
void Update () { if(Messaging.isConnected == true && connected == false) { connected = true; receiveThread.Start(); } }
}
If existingplayers is null or playerid is greater than the length of existingplayers then the code will thrown exception. Try running in the $$anonymous$$onoDevelop debugger, or add more Debug.Log messages to see the values of existingplayers, playerid, etc.
Answer by Eric5h5 · Feb 04, 2011 at 12:47 AM
You can't use any Unity API in threads, only Mono API.
Is there any workarounds for threading then? Somehow without threading the game started passed one frame after it had recieved message from server.
Threads do allow calls to Debug.Log. Other than that, true, no Unity API.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102730im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Stop Script Completely. Doesn't Execute Another Line Of Code. 1 Answer
How to make a pause function? 3 Answers
animation on multiple objects 1 Answer
Stop audio loop 1 Answer