# How to get a Read-Write Reference to Parent GameObject from a script component attached to it?

I have a game object(object) with a script component(myscript) attached.

I have a reference to myscript component through getComponent, and I want to change the transform of the gameObject the script is attached to.

myscript.gameObject.transform = (new value);


The above code gives me error,

Property 'UnityEngine.GameObject.transform' is read only.

Is there a way to get a read-write version?

• hmm.. it seems that I can just use myscript.transform but it also gives the same read-only error. – onguarde Nov 6 '12 at 9:42
• What value do you want to change? Position? Try assigning to transform.position. Local rotation? Try assigning to transform.localRotation. – Eric Nov 6 '12 at 9:49

Transform is read-only C# property. I guess it could be defined pretty most this way:

public Transform transform{get;}


So, you can't assign directly a Transform to another Transform (you don't have set accessor defined). You are forced to assign individual fields of each Transform to the other.

For example:

myscript.gameObject.transform.rotation = otherScript.gameObject.transform.rotation;
myscript.gameObject.transform.position = otherScript.gameObject.transform.position;
...

• FIY, it doesn't have a setter defined at all, and getter reads the value from native code. – Nevermind Nov 6 '12 at 13:08

If by "change the transform" you mean "change position/rotation/scale" of my object, you have to change these individually:

gameObject.transform.position = ...;
gameObject.transform.rotation = ...;
gameObject.transform.localScale = ...; // note that you CAN'T change global scale, as it's not actually a vector


If by "change the transform" you mean "change my object's parent", you have to change parent propery:

gameObject.transform.parent = someOtherTransform;