Search Unity

Why is Rigidbody first?

Discussion in 'Community Learning & Teaching' started by CyrJeff, Jul 19, 2017.

  1. CyrJeff

    CyrJeff

    Joined:
    Jul 18, 2017
    Posts:
    4
    I'm going through the documentation and trying to learn everything I can, while also compiling scripts and names in a big file to use as my custom reference. in this code

    void Start () {
    Rigidbody rb = GetComponent<Rigidbody>();

    // Add a force to the Rigidbody.
    rb.AddForce(Vector3.up * 10f); }

    Why is Rigidbody mentioned before "rb"? I get that rb becomes the variable we call up later to add force to the object it's attached to and that the component it modifies/affects is rigidbody as stated after getcomponent, but why is it stated before rb as well?

    P.s. sorry if this is the wrong section of the forums to post under, if it is let me know where I should have asked this and I'll post there in the future.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Rigidbody rb = GetComponent<Rigidbody>();

    This in English is saying "The type of Rigidbody will be used for the new variable called rb. This variable will then be given the result of the GetComponent command. This command looks for a component of the type of Rigidbody and returns a reference (where to look for it) which rb then holds".

    Let's break it down:

    Rigidbody rb is declaring a new variable called rb which is a type of Rigidbody. We could call it clowntron if we wanted:

    Rigibody clowntron = null; //this is a clowntron which can only be a Rigidbody, but currently it's null.

    Basically? learn C# first. It will make sense then.


    int a = 10;

    'a' can only be an integer from now on.

    float poo = 23.4;

    'poo' can only be a floating point number from now on.

    There's so much for you to learn but your biggest problem right now is learning C# not Unity. Because that would have answered what Types are and why they are important.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/

    (Rigidbody is a Custom Type) if reading that page... a custom type that Unity created for handling Rigidbody.
     
    theANMATOR2b and LaneFox like this.
  3. CyrJeff

    CyrJeff

    Joined:
    Jul 18, 2017
    Posts:
    4
    Ah, all right I assumed the tutorials and documentation would basically teach that. Any recommendations for documentation or tutorials to learn c#?

    Thanks for the help
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Tuts and docs do teach a lot but I think they might miss some fundamentals. Perhaps someone can recommend some nice online resources? (I can't because I taught myself a few decades ago so I've sort of just meandered along - learning to program once is sufficient to quickly migrate to new languages)..

    @LaneFox ;)
     
    LaneFox likes this.
  5. CyrJeff

    CyrJeff

    Joined:
    Jul 18, 2017
    Posts:
    4
    Right I sort of want self-teaching documentation, more of a "This is X and X does Y because Z" rather than "Do X then Y then Z and you have a calculator!" it's how I learned HTML like ten years ago. Though the difference is extreme, HTML hardly counts as coding as everyone keeps telling me
     
  6. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    It's not up to engine documentation to teach C# though - the docs for Unity are more of the reference variety, where you look up relevant information (as opposed to having every word in every sentence link to some sort of definition). ANY good C# basics book/tutorial will get you started, but there's now a decent chunk of such information provided by UT themselves.

    I see plenty of fundamentals in the Learn section on this site. Here's one on variables:
    https://unity3d.com/learn/tutorials/topics/scripting/variables-and-functions?playlist=17117

    But the primary two sources for C# information, basics, reference and tricks are Microsoft's own site and Dot Net Perls.
    (Yes, their spelling is questionable.)

    Scripting in Unity is really three parts: C#, the language has its own set of things you need to learn. Then there's the .NET API, which Unity uses most of up to a certain version. Then there's the Unity API, which follows a slightly different style at times (and is further divided into client side and editor APIs).

    It's a vast amount of information to digest, but if you have those few sources open you should be able to figure out just about any language/API issue. Graphics/game programming techniques are really the advanced tutorials relative to this. All language/API learning can be considered basic to intermediate (LINQ might be the hardest to wrap your head around if unfamiliar with the concepts).

    Also: A calculator can't be done in HTML. It's impossible as far as I know. Show me the markup if you know different ;)
     
    theANMATOR2b and hippocoder like this.
  7. CyrJeff

    CyrJeff

    Joined:
    Jul 18, 2017
    Posts:
    4
    Oh no I meant for learning c# the teaching method. it can't be done with html, maybe with html5 heck if I know the difference and for sure with javascript. Thanks for the help
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,529
    Not sure how I feel about this tag :p
     
    theANMATOR2b and hippocoder like this.
  9. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Desirable? Haunted?
     
    hippocoder likes this.
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Both, of course.

    Javascript in Unity is really UnityScript and it's being removed. So its a good idea to only learn C# or just use visual scripting as a future in Unity.