Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# Cheatsheet

Discussion in 'Scripting' started by Pyronide, Jan 19, 2015.

  1. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Does anybody know where I can get a good C# cheatsheet? I can work with JavaScript but I am wanting to learn C# as it is industry standard, but I was wanting to do so quickly so is there a cheatsheet that compares JS to C# where it would say things like: Var = public ___ for example... let me know.

    Thanks
     
  2. Zachary Van Kleeck

    Zachary Van Kleeck

    Joined:
    Oct 31, 2013
    Posts:
    40
    It doesn't really work like that, although that'd be pretty cool.

    You'll just have to get used to the different syntax. if you know JS, then leaning C# should come to you very quickly. They're very similar in a lot of ways.

    I would say make a simple project in JS, and then for some homework, convert it into C#. Eventually it'll come to you as easily as JS does.
     
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    Not exactly a cheat sheet, but the reference at MSDN contains most things. Both the keywords and the operator section contains examples of every one of them in use, so that'll get you a bit of the way.
     
    Kiwasi and jister like this.
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    some small things i can think of are:

    var number : int -> public int number;
    function Start(){} -> public void Start(){}
    transform.position.x = 1.5; is not possible in C# you would have to make a reference change that and reassign it to your transform or just use something like: transform.position = new Vector3(1.5f, transform.position.y, transform.position.z);
    the f or F is needed for a float.
    a raycasthit would need the out keyword as in Physics.Raycast(ray, out hit)

    I recommend watching something like this and see the real glory of C# and OOP :)

    also try it the other way around, convert C# scripts to JS and see what you have to change
     
  6. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Maybe this is a stupid suggestion, but Google has all your answers when it comes to syntax and good practices.

    For instance, if you forgot how to write a "function in C#", just google exactly that and the first example that will popup will tell you exactly how it works.
     
    Last edited: Jan 20, 2015
    Kiwasi likes this.
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    just convert your js and you'll pretty quickly realise what C# wants. Keep a few open unity C# scripts to refer to and examine / search the error messages. You'll find it's trivial to learn C# if you know js.
     
    Kiwasi likes this.
  8. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I do not know all there is to know about JS, but I know enough to understand what I am looking at for the most part... I want to learn C# because it is similar and because it is industry standard. May as well prepare myself now :D
     
  9. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    So jump in and learn it, just start writing, the logic is the same, they syntax can be learned on the go and the other features you can work on once you know how to do all your js stuff in c#
     
  10. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    That is what I am doing :D. I do have a question about that though, yes I posted it in another thread, but this one is getting the traffic :p.

    I am trying to make my shooter shoot once but it is instantiating uncontrollably. I was able to add a time delay in JS, but can't seem to figure it out in C# or find it in the newly upgraded manual :S... any ideas?
     
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
  12. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Code (csharp):
    1. Public Rigidbody bullet;
    2. Public Float speed;
    3. Public Float reload;
    4. Public Float lastShot;
    5.  
    6. void Start() {
    7. reload = 2;
    8. }
    9.  
    10. void Shoot () {
    11. if (Time.time = Time.time + reload) {
    12. Rigidbody bulletClone = (Rigidbody)Instantiate (bullet, transform.position, transform.rotation);
    13. rocketClone.velocity = transform.forward * speed;
    14. lastShot += Time.time + reload;
    15. }
    16. }
    17.  
    18. void Update() {
    19. if (Input.GetKey("d")) {
    20. Shoot();
    21. }
    22. }
    This is currently what I have.
     
  13. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    @Pyronide
    Can you do us all a favour ?
    In Monodevelop code can be formatted by Edit->Format->Format Document.
    It will make it easier to read and easier to visually check syntax and structure.

    Regards,
     
  14. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    When I copy and paste it in here I get nothing but wingdings for some reason...
     
  15. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    How did you do it in UnityScript. ?

    Perhaps using a coroutine and WaitForSeconds() will do the trick.

    Something like
    http://docs.unity3d.com/Manual/Coroutines.html
     
  16. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    It wasn't a coroutine, never heard of those and just read a little about it... head hurts :p

    I literally copied it from the old manual (I hate this new one) it used Time.time.

    it was something like taking the time instantiated and then adding the 'reload' variable and calling it something new like 'newShot' inside the end of the function.

    Code (csharp):
    1.  
    2. var newShot : float;
    3.  
    4. function Shoot () {
    5. if (time.time = newShot) {
    6. //instantiate code here.. this works for me too well :P
    7. newShot = time.time + reload;
    8. }
    9. }
    10.  
    again this is me typing into the box, not copying and I am unable to find the original source of this code ATM. This is JS not C# BTW.
     
  17. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    This won't work:

    Code (CSharp):
    1. if(Time.time=Time.time+ reload) {

    You're trying to assign a value to Time.time. You should be getting the error message:
    In addition, even if you replaced the = with the correct "==" (which is the syntax in JS too), it's a bad idea. == returns true only when the two values are exactly the same. If your user clicks the shoot button a millisecond too late, they can't shoot. You'll want to do:

    Code (CSharp):
    1. if (Time.time >= Time.time + reload) {
     
  18. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    thanks I will try this.
     
  19. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Sorry for double post but this does work now, thank you very much guys