- Home /
issue with yield.WaitForSeconds();
Ok, so I am having a problem with yield.WaitForSeconds but i may also be an issue with boolean referencing in a different script. Basically i have three states for my enemy: chase, patrol and deactivated. All of these states now work. The deactivated state is triggered by a boolean in attached to a console in the game. So when this console has been activated then the enemy is disabled. However i only want this to last a certain amount of time. Say 10 seconds. I tried using yield.WaitForSeconds(10); however for some reason this just makes my enemy spin around very slowly instead of going to it's waypoints.
var waypoints : Transform [];
var currentPoint : int = 0;
var state : String = "patrol";
var speed : float = 10;
var chaseDist : float = 15;
var gravity : float = 15;
private var controller : CharacterController;
private var player : Transform;
private var alert : ParticleEmitter;
function Start(){
alert = gameObject.Find("alert").particleEmitter;
player = gameObject.FindWithTag("Player").transform;
controller = GetComponent(CharacterController);
}
function Update () {
var playerDist = Vector3.Distance(player.position, transform.position);
if (playerDist <= chaseDist && GameObject.Find("consoletexture").GetComponent("consoleRange").consoleActive == false){
state = "chase";
//new state attempt//
}else if(GameObject.Find("consoletexture").GetComponent("consoleRange").consoleActive == true){
state = "deactivated";
}else {
state = "patrol";
}
if(state == "patrol"){
alert.emit = false;
renderer.material.color = Color.green;
if(currentPoint < waypoints.length) {
Mover(waypoints[currentPoint].position);
}
} else if (state == "chase"){
alert.emit = true;
renderer.material.color = Color.red;
Mover(player.position);
} else if (state == "deactivated"){
Debug.Log("enemy deactivated");
yield WaitForSeconds(10);
state == "patrol";
}
}
function Mover(target : Vector3){
var diffVector: Vector3 = target - transform.position;
var movement : Vector3;
if(diffVector.magnitude > 1){
movement = (diffVector.normalized * speed);
}else{
currentPoint = (currentPoint + 1) % waypoints.length;
}
movement.y -= gravity * Time.deltaTime;
controller.Move(movement * Time.deltaTime);
transform.LookAt(target);
}
so everything works perfectly until i added in the section:
Debug.Log("enemy deactivated");
yield WaitForSeconds(10);
state == "patrol";
You can't put a yield WaitForSeconds inside of Update- for something like this, I'd recommend making your own coroutine-based loop, which you call from Start (and then don't use Update).
Answer by TheFrankman123 · Nov 07, 2011 at 02:43 PM
I have figured it out.
} else if (state == "deactivated"){
Debug.Log("enemy deactivated");
Wait();
}
Then make the function.
function Wait () {
yield WaitForSeconds(10);
state == "patrol";
}
just make it a function outside the update. Thanks syclamoth. By telling me you can't use yield inside of an update i was able to figure this out.
That's not a good solution. As long as your "state" variable is "deactivated" you will start a new coroutine every frame! That means if you have a framerate of 60fps you would start 600 coroutines. After 10 sec the first one will set state to "patrol" and would stop the "coroutine spam". All the other coroutines will end within the next 10 sec and all will set state to "patrol".
This means, beside the incredible overhead, you can't set your state to something else for 10 sec.
Your answer
![](https://koobas.hobune.stream/wayback/20220613034129im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
(c#) Problems with yield 1 Answer
yield WaitForSeconds preventing further actions. 1 Answer
check bool in WaitForSeconds 2 Answers
yield WaitForSeconds makes GUI button disappear 1 Answer
How can I make a variable false for a period of time? 3 Answers