Using C#

Hi All,
I’m a c/c++ programmer so am wondering why unity uses c#? Is this for multiplatform reason?
One other related question, I’ve googled this but seem to be getting different answers can I use threads with c# in unity? Not to manipulated the graphics but to communicate with a server ran in the background, not a game server, a sort of IO server. Thanks

C# is a relatively easy language to use, and very popular. I don’t know why Unity chose to make use of it but as a C# developer by trade it’s very convenient for me.
As for threading, C# absolutely supports it. Look for information on the async keyword, that will get you started.

1 Like

I think the reason they went with C# was because of the Mono framework where C# is the primary language for it. Unity also started almost strictly as an indie tool. Non programmers can fairly easily learn to use C# whereas they will probably never learn how to use C++. Finally, being someone who learned programming by using C++ and I still use it on a monthly basis and also having several friends who have primarily used C++ to make games in the past, they all love C#. Give it a month or two and you might find yourself really enjoying it too.

As for threading, that would be under the System.Threading namespace. The async keyword is for internal Unity processes. All of Unity’s base data types are thread safe as well as their libraries.

  • Basic data types being things like:

Vector2, Vector3, Vector4, Matrix4x4, Color, Color32, Quaternion, etc.

  • Libraries being things like:

Mathf and any functions within the above data types.

  • What are NOT thread safe are components or any Unity Object:

GameObject, Material, Transform, Collider, Mesh, Texture, etc…

So for instance, if you want to manipulate a mesh using threads, you’ll want to grab all of the verts in the main thread, but manipulate them in separate threads, then reassign them on the main thread. Or, if you want to manipulate a texture you can grab the colors in the main thread, manipulate them on separate threads, and then reassign them back on the main thread.

Hope all that helps :slight_smile:

2 Likes