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 /
This question was closed Nov 16, 2021 at 08:31 PM by unity_418scarlet for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by unity_418scarlet · Nov 13, 2021 at 02:53 PM · coroutinecollider2dcomponentenabled

Coroutine set up but doesn't seem to be working, what am i doing wrong?

hello I have some trouble with this coroutine, from what i read around and in the manual it should be in the right format but unity tells me it cannot be started. I thought to use it to take away the collider in order to make the player invincible for half second after respawn but the collider never get active after that time. I did use a serialized game object for player cause unity does not allow to destroy a prefab, what am i doing wrong thank you

 [SerializeField] GameObject explosionPreFab;
     public GameObject player;
     [SerializeField] Vector3 respawnPoint;
     
     private void Start()
     {
         respawnPoint.z = -0.5f;
     }
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Lake"))
         {
             LevelManager.lives--;
             Destroy(gameObject);
             var explosionPlace = transform.position;
             var Explosion = Instantiate(explosionPreFab, explosionPlace, Quaternion.identity);
             Destroy(Explosion, 0.2f);
             player = Instantiate(player, respawnPoint, Quaternion.identity);
             player.GetComponent<Movement>().enabled = true;
             player.GetComponent<CapsuleCollider2D>();
             player.GetComponent<CapsuleCollider2D>().enabled = false;
             StartCoroutine(Inv());          
         }
         IEnumerator Inv()
         {
             yield return new WaitForSeconds(0.5f);
             player.GetComponent<CapsuleCollider2D>().enabled = true;
         }
 
     }


 
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

  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 13, 2021 at 03:29 PM

Those two lines do not work together:

 Destroy(gameObject);
 // [ ... ]
 StartCoroutine(Inv());

You start the coroutine on this gameobject's monobehaviour instance which you just destroyed. When you destroy a gameobject or deactivate it, all coroutines would die with it.


You have two options here. Either don't destroy this gameobject immediately and wait for the coroutine to finish, or the better approach, move the coroutine onto the player since it's only relevant to the player anyways. One of the point of using OOP is to have seperation of concerns. Currently you have another object (this one) to take care of a property change of another object (the new player object). While this object can initiate a certain action, the actual changes should be done on the new player object.


We don't know where this script is attached to, how it's named and in general what its purpose is since we only see a part of it. However I guess that player object you instantiate would have some behaviour scripts as well which could take care of the disabling and re-enabling of the collider


Finally it's a very bad habit to do

 player = Instantiate(player, ....

Assuming "player" is a prefab, you just reused the player member variable to hold the newly created instance. So the purpose of the variable changed and the original reference is lost. You really should avoid such usecases. It can easily being overlooked when you come back to the code in 6 month and suddenly something strange happens because you replaced that reference. Use local variables for such things.

Comment
Add comment · Show 1 · 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 unity_418scarlet · Nov 13, 2021 at 04:10 PM 1
Share

thank you Bunny, i am very new to all this , and i really appreciate the time you spent to show me the way.

this script is attached to the player, and i did put player = Instantiate... because if i did not do that unity was saying something about the impossibility of destroying a prefab, and if i wanted to do that i had to do destroy immediately.

i thought that the coroutine was referring to the new instance of the player not the one that just died.

if i get it , do i put this coroutine on the prefab so each time is respawned its collider will be off for the time due?

Follow this Question

Answers Answers and Comments

143 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

Related Questions

'enabled' is not a member of UnityEngine.Component 2 Answers

Convert Component to Actual Type C# to call a coroutine 0 Answers

Can I use a custom component as a parameter of a IEnumerator? 3 Answers

How to disable/enable Halo component via script 2 Answers

C# GetComponent, component not turning off. 2 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