I've parsed a string and if I print to log Debug.Log(GPSdata[2]); it does receive the appropriate data, however, I am unable to use the data as it is read only. I am trying to get GPSdata_received to populate a Gameobject's position and rotation, but it can't seem to get the information in the array to be useful. Any assistance is appreciated Edit: I have added the string format of the string below. $GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W * 70 The Editor error states: Assets\Serial.cs(38,17): error CS0200: Property or indexer 'string.this[int]' cannot be assigned to -- it is read only. Here is the code Code (CSharp): using UnityEngine; using System.Collections; using System.IO.Ports; public class Serial : MonoBehaviour { SerialPort stream = new SerialPort("COM5", 9600); public GameObject GPS; public Vector3 rot; public Vector3 rot2; public string receivedstring; public string[] GPSdata; public string[] GPSdata_received; void Start() { stream.Open(); //Open the Serial Stream. Debug.Log("Port open"); } void Update() { receivedstring = stream.ReadLine(); //Read the Serial Stream stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information. if (receivedstring.Contains("GPRMC")) { string[] GPSdata = receivedstring.Split(','); //Seperate stream by "," Delimiter foreach (var GPSdata_received in GPSdata) { GPSdata_received[0] = GPSdata[0]; // NMEA Format GPRMC GPSdata_received[1] = GPSdata[1]; // TimeStamp UTC-0 GPSdata_received[2] = GPSdata[2]; // Validity GPSdata_received[3] = GPSdata[3]; // Latitude GPSdata_received[4] = GPSdata[4]; // North/South GPSdata_received[5] = GPSdata[5]; // Longitude GPSdata_received[6] = GPSdata[6]; // East/West GPSdata_received[7] = GPSdata[7]; // Speed in Knots GPSdata_received[8] = GPSdata[8]; // True Course GPSdata_received[9] = GPSdata[9]; // Date Stamp GPSdata_received[10] = GPSdata[10]; // Variation GPSdata_received[11] = GPSdata[11]; // East/West } } } Changed my string parsing to : Code (CSharp): string[] GPSdata = receivedstring.Split(','); //Seperate stream by "," Delimiter for (int i = 0; i < dataLength; i++) { GPSdata_received[i] = GPSdata[i]; if (i == 2) { Debug.Log(GPSdata[i]); } }
You've named your iterator variable in the foreach the same as your class-level member, which I reckon is where the error stems from. The whole foreach feels superfluous as you're indexing the resulting array manually anyway. I don't know what format the data is in, you didn't give an example, but I imagine you want to use float.Parse or float.TryParse to convert it to "useable" data.
You are using the same var name for your member and your foreach loop, namely "GPSdata_received". The error makes sense since using it in the for loop means you are trying to write to the data you are reading from (which is readonly). Rename your member and put "GPSdata_received" to the right of the = sign and you should be fine.