0

I have been noticing scripting in C# in Unity is quite different than regular C# programming, especially the library! Can someone please explain why is this?

In 2 years or so I may want to turn my knowledge in C# into a actual career coding scripting in regular .NET (not making games career), but not sure since the whole scripting is completely different. Wouldn't I have to relearn a bunch of scripting and library?

Example

Console.WriteLine("Hello Stackoverflow"); THIS IS REG c#

print("Hello Stackoverflow");   THIS IS UNITY
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
motoDroid
  • 11
  • 3
  • If you can post the specific things you noticed that are different then you'll get a better answer otherwise this is too broad to answer. – Programmer Oct 28 '17 at 17:16
  • okay how about now? – motoDroid Oct 28 '17 at 17:17
  • `Console.WriteLine` and `print` are just log functions. Unity uses `print` to show log in it's Editor. It is simple a function and not a big deal. Another other real difference you noticed? – Programmer Oct 28 '17 at 17:18
  • Okay my simple question is will I be able to code in c# programming and not relying on unity script or library in 2 years? WITHOUT having to relearn some functions and libraries? Sorry a little new to c# – motoDroid Oct 28 '17 at 17:22
  • 1
    Not sure why would that be relevant, every framework/libraries has its own functions and classes etc, would would have to learn them either way if you want to use it. Regarding on C# you need to learn and understand the basics of the language itself such as syntax, OOP etc. Then you can apply it to any problem you are trying to solve. – penleychan Oct 28 '17 at 17:26
  • 2
    Console.WriteLine works only in Console applicaiton, other standard .net appliactions (like win forms, wpf, mfc) have their own alternatives, as does unity. You will need some additional knowledge to be able to work with unity engine, because it has its own classes and methods, but if you have a general knowledge of language, its syntax you will be able to code in anything using c#. Like for example Console and Debug are classes. First is used by console application to access it, second is used by unity and will give you an alternative to WriteLine - Debug.Log(). – Arman Papikyan Oct 28 '17 at 17:49
  • 1
    The underlying principles are the same though, they both work on c#, so if you understand how unity works, you will have no problem with programming on c#. – Arman Papikyan Oct 28 '17 at 17:50
  • All the functions and libraries you are familiar with are available in Unity (provided that the version of Unity is using the version of .NET you are familiar with: prior to Unity 2017, that would be .NET 2.0 whereas Unity 2017 is on some version of .NET 4). – Draco18s no longer trusts SE Oct 28 '17 at 20:00

1 Answers1

2

One of the biggest differences between C# and Unity's framework is the GameObject and Component system in Unity. Unity API is component based.

There is a GameObject and Component which can be attached to a GameObject. When you create a GameObject, you can attach a component to it with the AddComponent function. You can just assume that the component is your script/class.

In a normal C#, you can create new class instance with the new keyword:

ClassName clsVar = new ClassName();

With Unity Framework, you create new Component and attach it to an Object with the AddComponent function instead of the new if it is a component:

ClassName clsVar = gameObject.AddComponent<ClassName>();

You can also destroy that GameObject or Component with the Destroy.

Destroy(clsVar);

Another notable difference are constructors. It is not advised to use constructors in your scripts unless they do not inherit from MonoBehaviour.

Those are the biggest notable differences. Everything else remains the-same. If you learn C# console(No WPF/Winform needed) and understand the syntax, you can learn and understand Unity API within couple of days.

Okay my simple question is will I be able to code in c# programming and not relying on unity script or library in 2 years? WITHOUT having to relearn some functions and libraries?

Yes, but you will have to re-write your app with another framework. First of all, C# is a programming language. Unity is a platform that provides C# API that runs on devices other than Windows such as Android, iOS and MacOS. There are other alternatives such as Xamarin which has its own API too. You can also use WPF and Winform but they are only made to work on Windows. They all have difference API for doing something like playing sound, showing object on the screen and networking and you just have to relearn them if you switch to another one.

If you are using standard C# API that does not involve playing audio, graphics, it should also work in Unity. For example, the Socket, Thread, IO API's should work in Unity without any hard-work.

As long as you understand the C# language syntax, you just need to study the API of these platforms such as being able to remember class and functions names and what they are used for. Finally, if you need to start learning C# with Unity, you can do that with Unity's official C# tutorial which uses Unity to teach basic C# programming.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • *`"you can learn and understand the Unity API within a couple of days"`*- I very much doubt this. Just the API documentation alone contains hundreds of pages, and as you say, there are other issues to learn about such as portability between platforms, networking, threads, I/O, etc. A couple of years sounds far closer to reality for a novice programmer. – Ben Cottrell Oct 28 '17 at 18:04
  • @BenCottrell I am not saying you can learn C# and Unity in a couple of days.. I said once you understand C# language syntax which means when OP finish learning C#, learning Unity API is extremely easy especially with their doc and project [video tuts](https://unity3d.com/learn/tutorials), OP can even make and publish a complete game in couple of days by just swapping the assets from one those tutorials. There is no compatibly issue when it comes to networking, threads, I/O. It should just work. – Programmer Oct 28 '17 at 18:18
  • 1
    That's an entirely different proposition though - being able to build a working project using a template or sample code is not the same thing as learning or understanding. It's great that the tutorials make it so accessible and easy to get something working so fast - that certainly helps to smooth the learning curve, but I can't think of any subject where anybody can really gain anything more than a very superficial understanding in a few days; the nature of learning just takes a lot more time than that. – Ben Cottrell Oct 28 '17 at 18:24
  • I understand what you are saying but you are only given project files like sound and graphics. This is fair since a programmer don't have to be an audio engineer or 3D modeler. You can then use them to follow Unity programming tutorial which teaches basic GD stuff like detecting inputs, moving objects, collision, playing sound, accessing scripts from objects. Usually, I re-watched the videos if I didn't understand each lesson but it was easy since I learned basic C# before starting with Unity. When you create new project, you can always refer to your old one to see which API to use. – Programmer Oct 28 '17 at 18:32