- Home /
work around for transforms on Instantiated prefabs
Hi, I'm currently trying to make a simple tower defense game and i want my enemies to follow a set path in my game. I'm using an array of transform positions to achieve this. However when i try to save this to a prefab it doesn't save the transforms, so my instantiated enemies don't move when they spawn. Have know idea where to begin to solve this. Thanks!
#pragma strict
var forwardSpeed : float= 1;
var health : float = 100;
public var waypoint : Transform[];
private var pointA : Vector3;
private var currentWaypoint : int;
var levelMaster : LevelMaster;
private var maxHealth : float = 100;
function Start () {
levelMaster = GameObject.FindWithTag("LevelMaster").GetComponent(LevelMaster);
forwardSpeed *= levelMaster.difficultyMultiplier;
health *= levelMaster.difficultyMultiplier;
maxHealth *= levelMaster.difficultyMultiplier;
}
function Update () {
if(currentWaypoint < waypoint.length){
var target: Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude <1){
currentWaypoint++;
}
else{
velocity =moveDirection.normalized * forwardSpeed;
}
}
rigidbody.velocity = velocity;
//transform.Translate(Vector3.forward * (forwardSpeed * Time.deltaTime));
}
function TakeDamage(damageAmount : float)
{
health -= damageAmount;
if(health<= 0){
levelMaster.enemyCount--;
levelMaster.enemyCount+=(maxHealth + forwardSpeed*levelMaster.difficultyMultiplier);
//levelMaster.UpdateHud();
Destroy(gameObject);
}
}
function Slow(){
forwardSpeed *= 0.5;
yield WaitForSeconds(1);
forwardSpeed *= 2;
}
Can you try saving an array of Vector3 with positions? It doesn't look like you're using the rotation or scale anywhere else.
Answer by phodges · Oct 20, 2012 at 11:36 AM
I'm guessing that the transforms are part of the scene and not part of the prefab that you are saving to. If so, try rearranging your prefab to contain all of these transforms as well as the object that refers to them.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing children of instances vs children of original prefab 1 Answer
Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D) 1 Answer
rot not working for 2nd prefab 2 Answers
Why prefab revert does not work for position and rotation? 1 Answer
Can't remove instantiated prefab 0 Answers