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 natchoguy · Nov 30, 2013 at 10:03 PM · c#door

Why is StartCoroutine being ignored?

I created a door script. It's supposed to open the door, wait five seconds and then close. The opening and closing is done with animations.

Here's the problematic part of the code:

     IEnumerator DoorTime()
     {
         Time.timeScale = 1;
         yield return new WaitForSeconds (DoorTimer);
     }
 
     void changeDoorState()
     {
 
             theDoor.animation.CrossFade("Open");
             //theDoor.audio.PlayOneShot();
             doorIsClosed = false;
 
         StartCoroutine(DoorTime());
 
             theDoor.animation.CrossFade("Close");
             //theDoor.audio.PlayOneShot();
             doorIsClosed = true;
 
     }

and here's the full code:

 using UnityEngine;
 using System.Collections;
 
 public class DoorLogic : MonoBehaviour {
     
     public Transform theDoor;
     public GameObject DoorSideA;
     public GameObject DoorSideB;
     public GameObject RoomA;
     public GameObject RoomB;
     static float DoorTimer = 5.0f;
     private bool doorIsClosed = true;
     private bool drawGUI = false;
 
     void OnTriggerEnter(Collider theCollider)
         {
             if(theCollider.tag == "Player")
             {
                 drawGUI = true;
             }
         }
     
     void OnTriggerExit(Collider theCollider)
         {
             if(theCollider.tag == "Player")
             {
                 drawGUI = false;
             }
         }
     
     void OnGUI()
     {
         if (drawGUI == true)
         {
             GUI.Box( new Rect(Screen.width/2-51,200,102,22),"Press 'E' to Open");
         }
     }
     
     // Update is called once per frame
     void Update () 
     {
      if (drawGUI == true && Input.GetKeyDown( KeyCode.E))
         {
             changeDoorState();
         }
     }
 
     IEnumerator DoorTime()
     {
         Time.timeScale = 1;
         yield return new WaitForSeconds (DoorTimer);
     }
 
     void changeDoorState()
     {
 
             theDoor.animation.CrossFade("Open");
             //theDoor.audio.PlayOneShot();
             doorIsClosed = false;
 
         StartCoroutine(DoorTime());
 
             theDoor.animation.CrossFade("Close");
             //theDoor.audio.PlayOneShot();
             doorIsClosed = true;
 
     }
 
 }

please help

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by robertbu · Nov 30, 2013 at 10:07 PM

When you hit the yield on line 51 of the second script DoorTime() returns immediately. One way to fix your code, is to make 'changeDoorState' your coroutine:

 IEnumerator changeDoorState()
     {
  
          theDoor.animation.CrossFade("Open");
          //theDoor.audio.PlayOneShot();
          doorIsClosed = false;
  
          Time.timeScale = 1;
          yield return new WaitForSeconds (DoorTimer);
  
          theDoor.animation.CrossFade("Close");
          //theDoor.audio.PlayOneShot();
          doorIsClosed = true;
     }

Then on line 44 you would start the coroutine rather than calling the function.

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 iwaldrop · Nov 30, 2013 at 10:16 PM 0
Share

Also, because the animation time of the door is playing while you're counting down, you're not getting a full 5 seconds (or whatever DoorTimer is set to). You may consider adding this line of code after setting doorIsClosed to false (line 7 in code above at time of post):

 while (theDoor.animation.isPlaying)
     yield return null;

This will ensure that your door is fully open for the amount of time that you intend, regardless of animation changes (lengthening or shortening of animation duration by an artist).

avatar image natchoguy · Nov 30, 2013 at 10:46 PM 0
Share

thank you, that solved the problem, I'll refer to the answers more often

avatar image
0

Answer by MarkFinn · Nov 30, 2013 at 10:06 PM

When you yield from the co-routine, processing carries right on on the next line (After the StartCoroutine call). Only the code after the yield, within the coroutine itself waits for the timeout.

Move

  theDoor.animation.CrossFade("Close");
 //theDoor.audio.PlayOneShot();
 doorIsClosed = true;

inside the co-routine, placing it after the yield.

Comment
Add comment · 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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Simple OnGui does not work 1 Answer

Camera Follow for top down 2d shooter game 2 Answers

Turret Rotation Not correct 0 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