Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by PingoNg · Oct 30, 2015 at 09:08 PM · coroutineif-statementsstartcoroutine

StartCouroutine doesn't work with If statements?

So I have it set so that the player is static located at one part of the map, the enemy is moving towards the player using a nav mesh, so I calculate the distance before it's near the player then starts shooting at the player. Problem here is couroutine initiates right at the beginning and prevents me from using a if statement to control it. Thanks for your time reading this!

 using UnityEngine;
 using System.Collections;
 
 public class NavigationScript : MonoBehaviour 
 {
 
     public float distance;
     public GameObject player;
     private NavMeshAgent agent;
     public Rigidbody bullet;
     public float fireSpeed;
     public Transform posFire;
     public float delay = 2;
 
 
     // Use this for initialization
     void Start () 
     {
         player = GameObject.FindGameObjectWithTag("Player");
         agent = GetComponent<NavMeshAgent>();
         StartCoroutine (Shooting());
     }
 
     
     // Update is called once per frame
     void Update () 
     {
         GetComponent<NavMeshAgent>().destination = player.transform.position;
         distance = Vector3.Distance(player.transform.position, agent.transform.position);
     }
 
     IEnumerator Shooting()
     {
         if(distance < 160.0f)
         {
             for(int i = 0; i < 100; i++)
             {
 
                 Rigidbody SpawnBullet = Instantiate(bullet, posFire.position, transform.rotation) as Rigidbody;
                 SpawnBullet.velocity = transform.TransformDirection(new Vector3(0,0,fireSpeed));
                 yield return new WaitForSeconds (delay);
             }
         }
     }
 }
 
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
0

Answer by HenryStrattonFW · Oct 31, 2015 at 12:30 AM

The problem here is that the coroutine will immediately complete and exit if the first time its called the distance is greater or equal to 160. since the if statement will fail, the code will run to the end of the routine and it will finish.

Coroutines can be used like Update but unlike update, they can finish. If you want to continue running the coroutine all the time like Update. Then just wrap the entire if statement in a while(true) loop. with a "yield return null; " at the end of it.

EDIT: Re-looking at your code, you may actually want to move your seconds delay to replace the return null. That will cause the coroutine to run with the slower frequency. if that is what you're going for.

 IEnumerator Shooting()
 {
      while(true)
      {
           if(distance < 160.0f)
           {
                // Your other stuff here.
           }
           yield return null;
      }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

StopCoroutine not working. 0 Answers

Performance Issue with Coroutines 2 Answers

After stop coroutine it resumes its task when I call start coroutine 1 Answer

Can Unity throw an error when I start a coroutine without StartCoroutine? 0 Answers

How do I generate a random number in regular time intervals? 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