Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheFrankman123 · Nov 07, 2011 at 01:57 PM · booleanreferenceyieldwaitforseconds

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";
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Nov 07, 2011 at 01:59 PM 1
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Nov 07, 2011 at 02:43 PM 1
Share

Good job!

avatar image Bunny83 · Nov 07, 2011 at 04:58 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges