TCP/IP Unity Android ERROR Helfe
Hey all I am making a android game using unity3d, I want to control my game from my laptop I made a TCP/IP chat like program to get the input from my keyboard to my android over internet I made one for my Laptop (Windows form APP) and one for Unity, everything works except for one little annoying error.
ERROR: I want when I press the X button to move a sphere from point A to point B but when send the "x" value it says :
INTERNAL_get_position can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
and when I close the Windows form APP the ball then moves what to do I tried the Awake and the start functions too.
PS: I am testing this on the same computer so its not about the IPAddresses.
here is my code for the unity part:
using UnityEngine;
using System.Collections;
using System.Text;
using System;
using System.Net;
using System.Net.Sockets;
public class CHAT : MonoBehaviour {
private Socket sck;
EndPoint epLocal, epRemote;
//Gameobjects
public Transform ball , point;
//logic
static Boolean arreivedX = false;
string xIsHere;
// Use this for initialization
void Start () {
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
epLocal = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("81"));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse("192.168.1.9"), Convert.ToInt32("80"));
sck.Connect(epRemote);
Debug.Log("COnnected");
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCAllBack), buffer);
}
void Awake()
{
}
// Update is called once per frame
void Update () {
if (arreivedX)
{
ball.position = Vector3.MoveTowards(ball.position, point.position, 5 * Time.deltaTime);
}
}
private void MessageCAllBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] receivedData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
//bn3mal if statement bnshof weslat el X wela la2 w iza weslat bnmasi el tabeh
if (receivedMessage.Contains("x"))
{
Debug.Log("X is here");
arreivedX = true;
}
//b3deen bntba3 el msg bs b7aletna bdna n5li el touch active.
//ListMessage.Items.Add("Sender:" + receivedMessage);
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCAllBack), buffer);
}
catch(Exception exp)
{
Debug.Log(exp.ToString());
}
}
}
The error messages complains about an issue related to threading. I assume that has something to do with the asynchronous BeginReceiveFrom that you're trying to perform.
Try to do a synchronous receive first, and then check whether the error still occurs. If not, try to figure out which lines causes the error to happen.