childTransform.position = new Vector3(0, -1, 0);
위 코드에서, childTransform은 참조 타입이고 new Vector3는 구조체로 값 타입이다.
당연히 불가능하다. 하지만 childTransform이라는 변수는 Transform 타입을 할당한 변수이며, transform 컴포넌트의 “.position” 은 Vector3를 속성으로 가진다. 즉 childTransform.position은 값 타입이다. 참조 타입의 속성이 값 타입인 경우 문제가 발생하지 않는다.
그렇기에 같은 값 타입은 Vector3를 할당해도 문제가 발생하지 않는다. 다른 방법으로는 다음과 같은 방법이 있다.
// 값 타입을 참조 타입으로 감싸는 변환
Transform myTransform = new GameObject().transform;
Vector3 position = new Vector3(1, 2, 3);
myTransform.position = position;
참조와 값 타입의 변환 규칙을 이용하여 할당한 방법이다.