Movement code question

using UnityEngine;

using System.Collections;

public class AppleTree : MonoBehaviour

{

public float speed = 1f;

void Update ()
{
	// Basic Movement
	Vector3 pos = transform.position;
	pos.x += speed * Time.deltaTime;
	transform.position = pos;
	// Changing Direction;
}

}
Why do we need to type transform.position = pos;?
Didnt I set pos.x += speed * time.deltatime?
can someone explain this? I just started to learn c# so im a newbie.

Vector3s are structs, which means that any time you assign one, you get a copy not a reference to the original. Some things Unity like Vector3s, Quaternions, Bounds, Rects are structures. Most other things like any component you write, Colliers, Rigidbodies, Transforms, etc. are by reference.

So in your code, when you assign to ‘pos’ you are making a copy. After you modify ‘pos’ you have to copy it back.

Vector3 is a structure, and structure’s are passed by value, not by reference.

When you do “transform.position”, you’re using a getter to access the structure. But again, structures are passed by value, so “pos” is actually a copy of transform.position.
When you modify “pos.x”, you are modifying a variable belonging to this copy and not the transform.position.

This is why you have to assign “pos” back to “transform.position”, which is just passing “pos” by value to “transform.position” .

Also, when I say “pass by value”, you can think of it as just copying all the values within one structure to another.