Hello guys I just found a problem that I can’t seem to solve. I want to stop an enemy shooting script throught another script, but even when the sript is disabled the while cycle continues to do its job. Any ideas how to fix this? Here are my scripts.
EnemyBehaviour script
#pragma strict
var enemySpeed = 5;
var mooving = false;
function Start(){
gameObject.Find("EnemyBulletSpawner").GetComponent(SpawnEnemyBullets1).enabled = false;
}
function Update() {
if(mooving == true){
transform.position += Vector3.left * Time.deltaTime * enemySpeed;
}
}
function OnTriggerEnter(collision : Collider){
if(collision.gameObject.tag == "ActivateEnemy") {
print("The enemy is atacking you!");
mooving = true;
gameObject.Find("EnemyBulletSpawner").GetComponent(SpawnEnemyBullets1).enabled = true;
}
}
function OnTriggerExit(collision : Collider){
if(collision.gameObject.tag == "ActivateEnemy"){
print("The enemy is far away from you");
mooving = false;
gameObject.Find("EnemyBulletSpawner").GetComponent(SpawnEnemyBullets1).enabled = false;
}
}
SpawnEnemyBullets1 script
#pragma strict
var time = 3;
var prefab : Rigidbody;
function Start()
{
SpawnEnemyBullets();
}
function SpawnEnemyBullets () {
while(true)
{
Instantiate(prefab, transform.position, transform.rotation);
yield WaitForSeconds(3);
}
}
Thank you for help.
Yeah that didn't used to happen I seem to remember. Anyway put your Instantiate in an if(enabled) { }
– whydoidoitOk I changed if(true) to if(enabled) and now it just stoped shooting.
– TomatoOrgyLTwhile(true) { if(enabled) { Instantiate(prefab, transform.position, transform.rotation); } yield WaitForSeconds(3); }
– whydoidoitThank you! It works great now!
– TomatoOrgyLT