c# - Unity 3D Instantiated Prefabs stop moving -
i set pretty simple scene prefab being instantiated every x seconds. applied transform.translate on instances in update()
function. works fine until second object spawned, first 1 stops moving , instances stop @ translate value.
here script, attached empty gameobject:
using unityengine; using system.collections; public class test : monobehaviour { public gameobject prefab; private transform prefabinstance; void spawnenemy() { gameobject newobject = (gameobject)instantiate(prefab.gameobject, transform.position, transform.rotation); prefabinstance = newobject.transform; } void start() { invokerepeating ("spawnenemy", 1f, 1f); } void update () { if (prefabinstance) { prefabinstance.transform.translate (new vector3(4,0,0) * time.deltatime); } } }
your movement occuring on prefabinstance object in update(), however, object gets overwritten when second instance created, last instantiated prefab move.
you should consider splitting code 2 scripts, first 1 spawn prefab, , second script on prefab move it.
public class test : monobehaviour { public gameobject prefab; void spawnenemy() { instantiate(prefab, transform.position, transform.rotation); } void start() { invokerepeating ("spawnenemy", 1f, 1f); } }
and put script on prefab:
public class enemy : monobehaviour { void update () { transform.translate (new vector3(4,0,0) * time.deltatime); } }
Comments
Post a Comment