I’m making a game and I use one script to control multiple enemies of the same kind. It works properly but once in a while, the enemies become very rapid. I don’t know what causes this since this is my first work. Please… any ideas ?
We can only guess without a script example.
Thanks for the reply. In my game , all enemies of a type carry a script each containing only their individual variables and their OnTriggerEnter() functions. All enemies of a type have a manager that sets their variables and controls all of them in order to avoid many update functions running at a time. Below is a sample of a manager script for a group of enemies :
#pragma strict
var enemies : GameObject[ ];
var playerTag : String = “Player”;
function Start () {
enemies = GameObject.FindGameObjectsWithTag(“Enemy0”);
yield;
if(enemies != null){
for(var enemy in enemies){
if(enemy != null){
var scrip = enemy.GetComponent(Enemy0);
enemy.transform.position.x = Random.Range(-2.2, 2.2);
yield;
scrip.speed = Random.Range(0.8, 1.2);
}
}
}
yield;
}
function Update () {
var pl = GameObject.FindGameObjectWithTag(playerTag);
if(pl != null){
var playerM = pl.GetComponent(Transform);
}
var vari = FindObjectOfType(PlayerVariables);
var con = FindObjectOfType(GamePlayControl);
if(enemies != null){
for(var enemy in enemies){
/*var resty = Camera.main.transform.position.y - transform.position.y;
if(resty >= 4.2){
Destroy(gameObject);
}
transform.position = Vector3.MoveTowards(transform.position, playerM.position, speed * Time.deltaTime);*/
if(enemy != null){
var entrans = enemy.GetComponent(Transform);
var resty = Camera.main.transform.position.y - entrans.position.y;
var scrip = enemy.GetComponent(Enemy0);
if(resty >= 4.2){
Destroy(entrans.gameObject);
}
if(playerM != null){
entrans.position = Vector3.MoveTowards(entrans.position, playerM.position, scrip.speed * Time.deltaTime);
}
}
}
}
}