Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 P3ngu · Aug 26, 2020 at 07:21 PM · 2d gametime.deltatimetimescaletimer-scriptyield waitforseconds

the following Code after "yield return new WaitForSeconds();" not executed

Its like this, i have project, with some cool items. One of them is the Black Hole and another one is the "Hulk mode". The Black Hole reduce the time (with the Time.timeScale = 0.3f; command) too 0,3f and the Hulk Mode sets the size of the GameObject to a bigger Size and cant get hurt from Enemies. But when I integrate my Black Hole item in the Game the command "yield return new WaitForSeconds();" wont work anymore (or the following code after it). Does anyone have some ideas what went wrong? Tips for my Scritping Skills are Welcome too ;D

here's the Code for the Black Hole:

 public class SlowMotion : MonoBehaviour 
 {
     
 
     IEnumerator OnCollisionEnter2D (Collision2D collision) 
     {
 
         if(collision.gameObject.tag == "rocket")
         {
             Debug.Log("SlowMotion startet");
             Time.timeScale = 0.3f;
             yield return new WaitForSeconds(2f);
             Time.timeScale = 1.0f;
             Debug.Log("SlowMotion Ende");
         }
     }
 }
 
 

here the code for the Hulk Mode:

     public GameObject pickupEffect;
     public float mutliplier = 1.4f;
     public float scale;
     public float duration;
     protected float Counter;
     float trueornot;
     BoxCollider2D collider;
 
     protected void CountCounter()
     {
         Counter = trueornot;
     }
     void Start()
     {
         trueornot = 1f;
         
     }
     void OnTriggerEnter2D (Collider2D other)
     {
         
         if(other.CompareTag("rocket"))
         {
             
             StartCoroutine(Pickup(other));
 
         }       
     }
     
     private IEnumerator Pickup(Collider2D rocket)
     {
 
         Instantiate(pickupEffect, transform.position, transform.rotation);
         
         pickupEffect.transform.localScale = new Vector3(2,2,2);
         
         
         GetComponent<BoxCollider2D>().enabled = false;
         rocket.transform.localScale = new Vector3 (scale, scale, 1);
         
         rocket.gameObject.tag="Player"; 
         Debug.Log("1");
         yield return new WaitForSeconds(duration);
         Debug.Log("2");
         rocket.gameObject.tag="rocket"; 
         
         
         rocket.transform.localScale = new Vector3 (2, 2, 2);
         Destroy(this.gameObject);
     }
     
 }


I have a third Item, and with it I had some issues too, the position from the instantiated Objects were the same position where I collected the first Item. But this Issues got fixed by setting a "private" in front of the IEnumerator, maybe this comment is a help for the troubleshooting.


sincerely


Niklas

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
1
Best Answer

Answer by xxmariofer · Aug 27, 2020 at 06:50 AM

almost always this errors happens because you are deleting or desactivating the gameobject this script is attach to, take into account that if you delete the gameObject, it wont continue after the yield return

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 P3ngu · Aug 27, 2020 at 12:31 PM 0
Share

Alright, so how can I avoid this problem? $$anonymous$$aybe with a second script or should i put the "Destroy(this.gameObject);" behind the "StartCoroutine(Pickup(other));" ?

avatar image xxmariofer P3ngu · Aug 27, 2020 at 12:40 PM 0
Share

the Destroy(this.gameObject) is getting executed after the yield so you might be destroying to object somewhere else

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

251 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

Related Questions

Farming game Time management 0 Answers

Can somebody help me fix this script? 2 Answers

Increase a value by one every minute 2 Answers

AddForce with speed variations doesn't works properly? 0 Answers

PLEASE HELP!!! 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