gakutano
gakutano

Reputation: 1

Accessing the Properties in Unity3D

I want to ask why use ' sphere' to access transform.position which is sphere.transform.position in script1. But in script2, we can directly assess transform.Rotate without any objects before it, why not write as: sphere.transform.Rotate?

script1

void Update () {

}
private IEnumerator SphereIndicator(Vector3 pos)
{
    GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
    sphere.transform.position = pos;
    yield return new WaitForSeconds (1);
    Destroy(sphere)
}

script2

void Update () {
    if (axes == RotationAxes.MouseX) {
        transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityHor, 0);
    } 

Upvotes: 0

Views: 87

Answers (1)

Dávid Florek
Dávid Florek

Reputation: 571

As you can see here, .transform lets you access Transform component of the Game Object. If you just use transform, it is the same, as if you would use this.transform, it will return the Transform component attached to the same Game Object that the caller script is attached to.

So in your example, in the srcipt1 you are calling the Transform component from different Game Object, but in the script2 you are calling the component of the same Game Object.

Upvotes: 3

Related Questions