Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers

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;
...
share|improve this answer
    
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
    
@Nevermind: thanks for have pointed it out. –  Heisenbug Nov 6 '12 at 14:12
add comment

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;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.