Spawned units disappear

Hello Unity Community, I am new here, but i was searching for a while about why my spawned units just disappear or seems to teleport from the location they r born. Sorry for my english. But hope someone can help me with that issue.


Here is the code i got:

// Add this script to a Parent GameObject of the spawnPoints.

// Note: enemyPrefab will have an AI script attached which will already Tag the Player object

// so it won’t be needed here.

var spawnPoints : Transform; // Array of spawn points to be used.

var enemyPrefabs : GameObject; // Array of different Enemies that are used.

var amountEnemies = 20; // Total number of enemies to spawn.

var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.

var yieldTimeMax = 5; // Don’t exceed this amount of time between spawning enemies randomly.

function Start()




function Spawn()


for (i=0; i<amountEnemies; i++) // How many enemies to instantiate total.


  yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.

  var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.

  var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.

  Instantiate(obj, pos.position, pos.rotation); 



well thats simple the enemys arent teleporting there just spawning at a random locations so if u have more than one spawn just delete the

var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];

Sorry, i found the problem, but couldnt solve it, its my random movement script, right now i am using another one but not so good as the one i got this problem.

Thanks for the answers!!

Here is the code:

enum Type2D {XY,XZ,YZ}

var Type2D : Type2D = Type2D.XZ;

var yourMovementObject : Transform;

var randomRate = 1.0;

var moveSpeed = 1.0;

class MaxPosition


var UpMaxPositionX = 5;

var DownMaxPositionX = -5;

var UpMaxPositionY = 5;

var DownMaxPositionY = -5;

var UpMaxPositionZ = 5;

var DownMaxPositionZ = -5;


var MaxPosition : MaxPosition;

private var RandomX : int;

private var RandomY : int;

private var RandomZ : int;

function Update ()




switch (Type2D)


    case Type2D.XY:

        yourMovementObject.Translate(Vector3(RandomX,RandomY,0) * moveSpeed * Time.deltaTime,Space.World);


    case Type2D.XZ:

        yourMovementObject.Translate(Vector3(RandomX,0,RandomZ) * moveSpeed * Time.deltaTime,Space.World);


    case Type2D.YZ:

        yourMovementObject.Translate(Vector3(0,RandomY,RandomZ) * moveSpeed * Time.deltaTime,Space.World);



if (yourMovementObject.transform.position.x > MaxPosition.UpMaxPositionX)

    yourMovementObject.transform.position.x = MaxPosition.UpMaxPositionX;

else if (yourMovementObject.transform.position.x < MaxPosition.DownMaxPositionX)

    yourMovementObject.transform.position.x = MaxPosition.DownMaxPositionX;

if (yourMovementObject.transform.position.y > MaxPosition.UpMaxPositionY)

    yourMovementObject.transform.position.y = MaxPosition.UpMaxPositionY;

else if (yourMovementObject.transform.position.y < MaxPosition.DownMaxPositionY)

    yourMovementObject.transform.position.y = MaxPosition.DownMaxPositionY;

if (yourMovementObject.transform.position.z > MaxPosition.UpMaxPositionZ)

    yourMovementObject.transform.position.z = MaxPosition.UpMaxPositionZ;

else if (yourMovementObject.transform.position.z < MaxPosition.DownMaxPositionZ)

    yourMovementObject.transform.position.z = MaxPosition.DownMaxPositionZ;


function RandomlyMove ()


RandomX = Random.Range(-2,3);

RandomY = Random.Range(-2,3);

RandomZ = Random.Range(-2,3);