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 Bill Cosby · Nov 28, 2012 at 06:35 AM · c#booleanyieldwaitforsecondsvoid

(c#) Problems with yield

Hello. I am trying to write a script where hitting the Fire1 button sets a boolean value called _isAttacking to true, then sets it to false five seconds later. The relevant part of the code is at the bottom in the void Attack() block of code, but here's the whole thing just incase something needs to be changed elsewhere:

 using UnityEngine;
 using System.Collections;
 public class movement : MonoBehaviour {
 
     public float speed = 6.0F;
     public float jumpHeight = 8.0F;
     public float gravity = 20.0F;    
     private Vector3 moveDirection = Vector3.zero;
     CharacterController controller;
     public bool isAttacking { get { return _isAttacking; } set { _isAttacking = value; } }
     bool _isAttacking = false;
 
     
     void Start(){
 controller = GetComponent<CharacterController>();
 }
     void Update() {                    
         if (controller.isGrounded) {
               moveDirection = Vector3.forward;
               moveDirection = transform.TransformDirection(moveDirection);
               moveDirection *= speed;        
               
         
             
             if (Input.GetButtonDown("Jump"))
             {
                 moveDirection.y = jumpHeight;                
             }            
     
         }       
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }
     
     void Attack(){
         if (Input.GetButtonDown("Fire1"))
         {
             _isAttacking = true;
             yield return new WaitForSeconds(5);
             _isAttacking = false;
                 
         }
 }
 }

My problem is that the debugger is giving me an error that says "The body of `movement.Attack()' cannot be an iterator block because `void' is not an iterator interface type" and I'm not sure what that means, as I'm kind of a beginner with Unity Scripting. What should I do to get this working?

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

1 Reply

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

Answer by Flynn · Nov 28, 2012 at 06:41 AM

C# is a bit more strict with how you are supposed to do things than JS -- It wants your functions to be clearly defined as iterator blocks, and it wants you to strictly call them using StartCoroutine.

To see how this should be done, replace your Attack() method with this:

 public void Attack()
 {
     StartCoroutine(Attack_Coroutine());
 }
 
 private IEnumerator Attack_Coroutine()
 {
     if (Input.GetButtonDown("Fire1"))
        {
         _isAttacking = true;
         yield return new WaitForSeconds(5);
         _isAttacking = false;
        }
 }

From a code clarity standpoint, I recommend checking Input.GetButtonDown("Fire1"), and THEN calling Attack() if that returns true -- but that's purely methodology, and not required :)

Good luck!

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 loopyllama · Nov 28, 2012 at 12:38 PM 0
Share

I second that advice about calling attack after detecting the fire button has been pressed. +1 for Flynn.

avatar image Bill Cosby · Nov 28, 2012 at 06:34 PM 0
Share

Awesome! I took your advice of calling attack after the input is checked and it's working perfectly now! Thanks so much for your help!

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Waiting between pingpong loops 2 Answers

What is wrong with this use of WaitForSeconds? 1 Answer

C# yield waitforseconds 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