I am starting to learn some unity and I have a quick question in regards to multithreading.
I have a class which contains an instance of a datamodel class. The primary function of the class is the fetch a remote resource asynchronously and update the datamodel class. Then in the update function the datamodel class is read out and written to some text in a gameobject.
Anyway I am worrying that it could cause some issues related to multithreading since my async updating of the class could run at the same time as the update function causing a race condition. Do I have to wrap access to the class in a mutex?
class Data {
public int Number { get; set; }
public string Name { get; set; }
}
class Network : MonoBehaviour {
private Data d;
public void Start() {
// setting up handler to async fetch data and call provided callback
Networking.GetData(s => ParsePayload(s));
}
private void ParsePayload(string payload) {
d = JsonConvert.DeserializeObject<Data>(payload);
}
public void Update() {
var label = GameObject.Find("textObject").GetComponent<Text>();
label.Text = d.Name;
}
}
So am I right in this or does unity handle this by itself?
thx for any advice!