Search Unity

Scripting query: When to use assignment operators?

Discussion in 'Scripting' started by Deleted User, Mar 19, 2018.

  1. Deleted User

    Deleted User

    Guest

    I am very, very new to programming.

    I was following the 'Roll A Ball' tutorial and noticed that to rotate a cube the '=' sign wasn't used. this is the code


    void Update () {
    transform.Rotate (new Vector3 (15,30,45) * Time.deltaTime);
    }


    can someone explain to me why after transform.rotate aren't we adding an '=' sign? Isn't this technically a new value? Also, what's the difference between rotate and rotation in this context
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Okay, there may be people better suited to answering this, but I'll give it a clear shot.

    Basically rotation is a field, and Rotate is a method. You can tell the different between the two by one starting uppercase and the other starting lowercase. You'll also notice that the method needs backets like this Rotate(). When you're changing the value of rotation, that's setting the real world rotation of that object. It's just a number somewhere in the Transform class, and you're changing it. When you're calling Rotate(someVector3), that's a method. Methods aren't just a single value, they actually perform a function/ make an action. The action in the case of Rotate() is to change the rotation value. So when you do Rotate() that method will change the rotation value for you. When you change the rotation field's value, it will directly set the value to the rotation you wanted it to be. Both of these ways of changing an object's rotation works okay. It's just sometimes you might want to set the direct rotation, and sometimes you'll want to just rotate it a little left or right, so Rotate() might be more fitting, as Rotate() rotates it a little from the direction it's currently facing, while changing 'rotation' will set it to whatever you're asking it too, regardless of what it's rotation was before.

    As for the '=' sign, that's a way to say, "this value equals this". So on Rotate() you put the Vector3 you want it to be in the curly braces, while in a field you put an = sign and say what you want it to be.

    tl;dr - watch this video https://unity3d.com/learn/tutorials/topics/scripting/variables-and-functions?playlist=17117
     
    Last edited: Mar 19, 2018
    Deleted User likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Rotate is a method that acts on the transform (modifying its values).. in this case, the rotation.

    transform.rotation is a quaternion value. It's a property of the transform.

    Lots of general, introduction C# information can be found in the following section: https://unity3d.com/learn/tutorials/s/scripting

    Be sure to start and 1 and work your way through over time if you'd like to learn. :)