- Home /
Updating prefab information live
How can you update a prefabs position values live if it is already in game view?
For example, if i have a prefab of a cube and its XYZ position values are all set at 1. And if i place that prefab into the scene view, but its position values are being set all at 5 and its following way points. If i spawn an object into the game(via mouse click) and i want it to follow that cube, it will always go to the prefab coor'ds, meaning 1. How can i change that value so the cude will actually move towards it?
EDIT:
Here is what i am doing at the moment:
var target : Transform; var smooth = 5.0;
function Update() { transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * smooth);
}
Explanation: When my Player gameObject hits an enemy collider (which is bigger than the enemy gameObject) this script will be activated so a prefab(which is just a square at the moment) will 'fire' at the enemy target to destroy it.
Answer by DaveA · May 11, 2011 at 11:05 PM
Assuming you're doing something like the samples here: http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
You would have something like (from that page)
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation); clone.transform.position = whereever you want it
and use 'clone' in your Update script to set its transform.position to follow the cube
Answer by Bryan 4 · May 11, 2011 at 03:53 PM
It appears your spawned object are referencing the prefab itself and not the cube in game. On your object spawn you should have it just pull a reference to the thing you want it to follow in game. That will ensure it follows the correct object.