• 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 JimTheGhost · Oct 30, 2020 at 09:28 PM · scripting problemcoroutineprojectilespawning problems

My coroutine still finishes even after i stop it

Script A will start and maintain a timer to kill a projectile if its lived a certain amount of time, Script B will start and stop it based on weather its inactive due to being part of the players ammo or being dragged towards the player. For whatever reason by the time I try to use it as ammo even though it should have stopped the coroutine it has already died because of the coroutine

Script A:

   public void Initialize()
         {
             killTimer = StartCoroutine(nameof(KillProjectile));
             directionOfTravel = transform.forward;
         }    
     private void OnDisable()
         {
             StopCoroutine(killTimer);
         }
     
         private void OnEnable()
         {
             if(killTimer != null) return;
             killTimer = StartCoroutine(nameof(KillProjectile));
         }   
      public IEnumerator KillProjectile()
         {
             yield return new WaitForSeconds(timeToLiveInSeconds);
             if (isActiveAndEnabled)
             {
                 if (instigator == Instigator.Player)
                 {
                     Debug.Log("Broken");
                 }
                 Destroy(gameObject);
             }

Script B:

     private void ActivateSuction()
     {
 
         
         //for each projectile in range
         foreach (var projectile in coneDetection.targetsInRange)
         {
             //check if projectile is still alive
             if (projectile == null || !projectile.CompareTag("Projectile")) continue;
             var projectileController = projectile.GetComponent<Projectile>();
             
             //change projectile to act as players
             if (charStats.characterDefinitions.isPlayer)
             {
                 projectileController.instigator = Instigator.Player;
             }
             
             //change the direction of travel
             projectileController.directionOfTravel =
                 (charStats.transform.position - projectile.transform.position).normalized;
             if (!projectileController.isActiveAndEnabled) continue;
             //set time to live to 6 seconds and stop the kill timer
             projectileController.timeToLiveInSeconds = 6;
             projectileController.StopCoroutine(projectileController.killTimer);
             
             //restart the kill timer
             projectileController.killTimer = null;
             projectileController.killTimer = projectileController.StartCoroutine(projectileController.KillProjectile());
 
 
         }
 
     public bool AddToAmmoSack(GameObject ammoToAdd)
     {
         var ammoController = ammoToAdd.GetComponent<Projectile>();
         if (ammoSack.Count < ammoLimit)
         {
             ammoSack.Add(ammoToAdd);
             if (ammoController != null)
             {
                 ammoController.StopCoroutine(ammoController.killTimer);
             }
 
             return true;
         }
         else
         {
             Debug.Log("Sack Full!");
             return false;
         }
     }
 
     public void FireWeapon()
     {
         GameObject spawnedProjectile;
         Projectile projectile;
         //spawn a projectile, get its controller
         if (weaponDefinition.isSucker)
         {
             if (ammoSack.Count == 0)
             {
                 Debug.Log("No Ammo");
                 return;
             }
             //Get the first object in the ammo sack and reposition it
             spawnedProjectile = ammoSack.First();
             spawnedProjectile.transform.position = weaponDefinition.muzzleSlot.position;
             spawnedProjectile.transform.rotation = transform.rotation;
             //Remove the shot object from the ammo sack and activate it
             ammoSack.Remove(ammoSack.First());
             spawnedProjectile.SetActive(true);
             //get the projectile script and reinitialize it
             projectile = spawnedProjectile.GetComponent<Projectile>();
             projectile.Initialize();
             projectile.rigidbody.velocity = Vector3.zero;
             projectile.directionOfTravel = charStats.transform.forward;
         }
         else
         {
             spawnedProjectile = Instantiate(weaponDefinition.projectilePrefab, weaponDefinition.muzzleSlot.position,
                 transform.rotation);
             projectile = spawnedProjectile.GetComponent<Projectile>();
         }
         
         projectile.WasHit += DamageTarget;
             //change the owner based on if its a player or not
             switch (charStats.characterDefinitions.isPlayer)
             {
                 case true:
                     projectile.instigator = Instigator.Player;
                     break;
                 case false:
                     projectile.instigator = Instigator.Enemy;
                     break;
             }
             
     }


I really don't know what I'm doing wrong, it was working the other day and neither of these scripts were edited but i did update to the most recent 2019LTS version

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

326 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Damage calculation with various weapons 1 Answer

How can I start a Coroutine with the onClick of a button? 0 Answers

C# coroutine not waiting on waitForSeconds? 2 Answers

question about object pooling an array and spawning 0 Answers

Character Controller creates invisible wall where he spawns... 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