Search Unity

Getting Started with C#

Discussion in 'Scripting' started by wbl1, Dec 30, 2018.

  1. wbl1

    wbl1

    Joined:
    Apr 22, 2009
    Posts:
    156
    All,

    I've been working with UnityScript for several years and am basically familiar with the overall structure of Unity. But I don't have much experience with C# and clearly need to get started. Any advice on how to get going? I don't really want to spend a lot of time on tutorials that take me through lots of things that I already know - I really just need to know the basics of programming in C#.

    Any good books to read? Are their good tutorials that just focus on the programming?

    Thanks!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,599
    How about Unity's own tutorials on C#? Learn section is actually pretty extensive, it is possible to just read the code they have under tutorials, no need to watch videos if you already know UnityScript I guess.

    https://unity3d.com/learn/tutorials/s/scripting

    Then there are various non-Unity sources for C# Like dotnetperls and free books, just google for C# tutorials or C# book.

    Also, I don't know how different UnityScript is on specifics like events and delegates, anonymous methods and such, but all these you can find from standard C# tutorials.
     
    wbl1 likes this.
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    27,547
    Port your UnityScript code to C#. Don't write C#. Just port.

    This will teach you all you need to know about the subtle differences - they're basically exactly the same except for minor syntax differences and minor rules.

    Don't learn C#.
    Just port.


    Some quick tips:
    • Variable types are prefixed instead of put on the end
    • UnityScript secretly generates a class the same name as the filename without showing you. In C# you simply have to do that first, and it's one line of code, so not much to learn ( class whatever:MonoBehaviour)

    • You'll sometimes need to do things like:

      transform.position.x = 10;

      becomes

      Vector3 pos = transform.position;
      pos.x = 10;
      transform.position = pos;

      This is a minor inconvenience and can be disguised with extension methods to do the same as before if you want to, but I highly recommend you don't because you end up just writing code that shows what is really going on under the hood (US basically was bloatware syntax sugar running on top of Boo...)
    I've released titles using US and 2 using C#, so that's why I think that porting an existing project will teach you very quickly all you need to know - probably within a few days. The rest will be enforced with new habits.
     
    wbl1 and Ryiah like this.
unityunity