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 hexstonepatriot · May 02, 2017 at 11:10 PM · destroycoroutinegetcomponentontriggerenter

Coroutines WaitForSeconds question...

Can someone explain to me why this code is not setting my fire rate back to the correct value (the hard coded value)? I am still trying to understand Coroutines.

     public PlayerController playerController;
     public float totalPickupTime;
     public float bonusRate = .05f;
 
     private bool hasPickup;
 
     void Start()
     {
         playerController = playerController.GetComponent<PlayerController>();
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             GetComponent<MeshRenderer>().enabled = false;
             playerController.fireRate = bonusRate;
             StartCoroutine("PickupTime");
         }
     }
 
     IEnumerator PickupTime()
     {
         yield return new WaitForSeconds(totalPickupTime);
         playerController.fireRate = .25f;
         Destroy(gameObject);
     }

Thanks in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Matthewj866 · May 03, 2017 at 01:53 AM

Hi there.

Not sure if you're following a tutorial or something? Regardless, StartCoroutine("PickupTime"); is wrong. You cannot send it a string, it has no overload for that. You need to send it a direct call to PickupTime().

Example:

  private void OnTriggerEnter(Collider other)
  {
      if (other.tag == "Player")
      {
          GetComponent<MeshRenderer>().enabled = false;
          playerController.fireRate = bonusRate;
          StartCoroutine(PickupTime());
      }
  }

This should fix your problem. 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 Jawchewa · May 03, 2017 at 02:34 AM 1
Share

Actually, I'm pretty sure there is an overload of StartCoroutine that takes a string.

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

If you look at the last example on this page, it shows how you can pass in the name of a method as a string to start a coroutine. Although, I will definitely agree with you that passing it in as a method as in your example is generally the better way to go about doing it.

avatar image Matthewj866 Jawchewa · May 03, 2017 at 02:57 AM 0
Share

Well TIL, I've never done it that way and VS intellisense never showed that to me so I just sort of assumed.

avatar image
0

Answer by hexstonepatriot · May 03, 2017 at 02:10 AM

Hey @Matthewj866 thanks for responding. Unfortunately I didn't have luck. Here is what I did:

     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             GetComponent<MeshRenderer>().enabled = false;
             playerController.fireRate = bonusRate;
             StartCoroutine(PickupTime());
         }
     }
 
     IEnumerator PickupTime()
     {
         Debug.Log("Is Called");
         yield return new WaitForSeconds(totalPickupTime);
         Debug.Log("Is called again");
         playerController.fireRate = .25f;
         Destroy(gameObject);
     }


As you can see I added a couple print statements. The first one ("Is Called") shows up in the console. However the second one ("Is called again") never does. For reference totalPickupTime is set for 2 seconds. I am really not sure what I am doing wrong.

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 Jawchewa · May 03, 2017 at 02:40 AM 0
Share

From what I can see, your code seems like it should be working, especially if the first debug message is appearing like you said. I'm kind of wondering if you are somehow setting totalPickupTime incorrectly. $$anonymous$$aybe try hardcoding the number 2 into WaitForSeconds, and see if that works. If it does, it might be something with how that variable is being set.

avatar image hexstonepatriot Jawchewa · May 03, 2017 at 02:47 AM 0
Share

Thanks for responding. I tried WaitForSeconds(2). Its like it is skipping some of the code because it does destroy the pickup object after 2 seconds. But it doesn't set my players fire rate nor does it print to the console.

avatar image
0

Answer by BigScary · May 06, 2017 at 08:56 PM

I've had this problem before. In my case, something elsewhere in my code had set the object/script containing the coroutine inactive. If your script or its containing object are inactive when the coroutine should continue, it won't.

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

108 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

Related Questions

Can I check if this.gameObject was destroyed? 5 Answers

Destroying GameObject after amount of triggers 2 Answers

how do i destroy all object in the same group when trigger?, 0 Answers

Delete object with key within Trigger 1 Answer

Missile Boucne Issues (need a creative solution) 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