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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by j0ffe · Oct 27, 2012 at 10:54 AM · c#ienumeratorbool

Wait for seconds and then set a bool to true.

So I have ma de this script for my movement but i got the problem that IEnumerator time() won't run. I have tried to write it as. IEnumerator bool time() but is doesn't work either. Thanks for your help.

using UnityEngine; using System.Collections;

 public class movement : MonoBehaviour {
     
     //value for movement with keys
     public float movspd = 10.0f;
     public float trnspd = 100.0f;
     
     //value for push movements
     public float push = 10.0f;
     public bool quickpush = true;
     
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         // This is the movement for the arrows 
         float move_Forward = movspd * Time.deltaTime * Input.GetAxis("Vertical");
         float rotate = trnspd * Time.deltaTime * Input.GetAxis("Horizontal");
         
         transform.Translate (Vector3.forward * move_Forward);
         transform.Rotate (Vector3.up * rotate);
         // Ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         
         
         // This is the movement for the sida pushes
         float push_p = push * Time.deltaTime;
         float push_n = -push * Time.deltaTime;
         
         if(Input.GetKey("a") && quickpush){
             transform.Translate (Vector3.right * push_p);
             quickpush = false;
             time();
         }
         
         if(Input.GetKey("d") && quickpush){
             transform.Translate (Vector3.right * push_n);
             quickpush = false;
             time();
         }
         // Ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         
         
         if( Input.GetButton("reset")){
             transform.position = new  Vector3(0,0,0);
             transform.eulerAngles = new Vector3(270,0,0);
         }
     }
     
     IEnumerator time(){
         
         yield return new WaitForSeconds(2);
         quickpush = true;
         return;
     }
 }
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 Fattie · Oct 27, 2012 at 12:01 PM 0
Share

just use Invoke for simplicity, when you're learning

unityGems.com

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xroft666 · Oct 27, 2012 at 10:57 AM

You should create a new Coroutine using function

StartCoroutine( time() )

ps: but think twice before creating a coroutine in update function. The code will create one coroutine per frame

Comment
Add comment · Show 3 · 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 fafase · Oct 27, 2012 at 11:02 AM 1
Share

You beat me to it.

Actually he has the quickpush set to false so it won't call it each frame.

avatar image j0ffe · Oct 27, 2012 at 11:15 AM 0
Share

Thanks to the booth of you two! It works!

avatar image Xroft666 · Oct 27, 2012 at 11:17 AM 0
Share

fafase

yea, you're right.. )

avatar image
1

Answer by fafase · Oct 27, 2012 at 11:01 AM

Try with:

 StartCoroutine(time());

I would deeply recommend as well to avoid using name of objects that already exist. time is a variable from Time and could get you confused as which one you are using (despite the () for the function). Get used to using clear different names.

Comment
Add comment · Show 1 · 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 j0ffe · Oct 27, 2012 at 11:16 AM 0
Share

Thanks for the tip I will think of it in the future.

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

12 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

Related Questions

IEumerator not recognising when a bool has switched 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Acessing animator on another Object 1 Answer

Make a laser flickering. 1 Answer


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