- Home /
What threads are used in a scene?
I'm designing the general architecture of my game, using c#, and I've implemented a singleton pattern, which I hope will facilitate communication between some of my game objects.
My Question, if I add the same script to a bunch of GameObjects, and the script calls functions on the singleton, would the singleton need to be thread safe? That is, all the GameObjects would then have similar functionality, all calling the same function, the same variables in that function, would I need to use lock() (or something similar) when changing values in the singleton from the scripted GameObjects?
Do you create other threads in you game ?
If you do not, there will not be any issue according to me. I mean you need to lock only if you think that two processes will access the same block in the same time. And you want the block to have a single process access.
Just in reading your question, I would say no, you do not need to lock code in singleton.
Answer by RudyTheDev · Feb 27, 2014 at 12:17 PM
Unity is not "thread safe" in the sense that it doesn't actually let you work with its objects from different threads. If you try, it will throw up an error.
In your case, every GameObject will be on the same thread and it's safe to access your singleton from any other GameOject. You don't need to lock anything, because no Unity stuff, no Update()s or other events will run concurrently. Unless you make you own threads, everything will happen sequentially (although the order is not guaranteed).
See also: This answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134901im_/https://answers.unity.com/themes/thub/images/avi.jpg)