x


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";
more ▼

asked Nov 07 '11 at 01:57 PM

TheFrankman123 gravatar image

TheFrankman123
162 32 35 37

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).

Nov 07 '11 at 01:59 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Nov 07 '11 at 02:43 PM

TheFrankman123 gravatar image

TheFrankman123
162 32 35 37

Good job!

Nov 07 '11 at 02:43 PM syclamoth

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.

Nov 07 '11 at 04:58 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x461
x395
x345
x238

asked: Nov 07 '11 at 01:57 PM

Seen: 1668 times

Last Updated: Nov 07 '11 at 04:58 PM