- Home /
moving an instance of a gameObject
I have a prefab I'm trying to instantiate and than move. I was able to instantiate it using :
clone = (GameObject) Instantiate ( object_ );
where both clone and object are GameObjects. Im trying to move clone using
clone.transform.position.x += 20;
How ever Im getting an error : Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable
How can i fix that?
Answer by Jesse Anders · Feb 02, 2011 at 11:55 PM
Assuming this is C#, you have to write something like this (note that this is just one way to do it):
clone.transform.position = new Vector3(
clone.transform.position.x + 20,
clone.transform.position.y,
clone.transform.position.z
);
It would probably be easier just to use Transform.Translate() though.
Just out of curiosity would adding time.deltatime make it go slowly and not just telephoto to that poaition.
Answer by Myth · Feb 03, 2011 at 12:02 AM
and here is a .js option - using for bullets, but they are the rigid body objects.
var bullit1 = Instantiate(bulletPrefab,transform.Find("spawnPoint1").transform.position, transform.Find("spawnPoint1").transform.rotation);
bullit1.rigidbody.AddForce(transform.forward * 3600);
guntime = gunspeed;