Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[URGENT HELP]GameObject is already being activated or deactivated

Discussion in 'Scripting' started by hamzah2412, May 9, 2020.

  1. hamzah2412

    hamzah2412

    Joined:
    Jul 8, 2019
    Posts:
    9
    The console keeps showing this error for days and it comes to the script:

    Code (CSharp):
    1. void OnCollisionExit2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "MovingPlatform")
    4.         {      
    5.              gameObject.transform.parent = null;
    6.         }
    7.     }
    I am making a tag system mechanic that involves deactivating and activating 2 character gameobject in unity. reply asap
     
    Last edited: May 10, 2020
  2. elliot_eserin

    elliot_eserin

    Joined:
    Jan 5, 2020
    Posts:
    38
  3. hamzah2412

    hamzah2412

    Joined:
    Jul 8, 2019
    Posts:
    9
    Im still having issues: the messages still appear and now my characters drop through the ground and shows GetShape == null?? What is this??! Help please anyone im going insane

    Code (CSharp):
    1.  void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "MovingPlatform")
    4.         {
    5.             gameObject.transform.parent = collision.transform;
    6.             Collider2D[] colliders2 = GetComponentsInChildren<Collider2D>();
    7.             foreach (var collider in colliders2)
    8.             {
    9.                 collider.enabled = true;
    10.             }
    11.         }
    12.         if (collision.gameObject.tag == "Ground")
    13.         {
    14.             Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
    15.             foreach (var collider in colliders)
    16.             {
    17.                 collider.enabled = true;
    18.             }
    19.         }
    20.     }
    21.     //CollisionExit for player exiting MovingPlatform.
    22.     void OnCollisionExit2D(Collision2D collision)
    23.     {
    24.         if (collision.gameObject.tag == "MovingPlatform")
    25.         {      
    26.              gameObject.transform.parent = null;
    27.             Collider2D[] colliders2 = GetComponentsInChildren<Collider2D>();
    28.             foreach (var collider in colliders2)
    29.             {
    30.                 collider.enabled = false;
    31.             }
    32.         }
    33.         if (collision.gameObject.tag == "Ground")
    34.         {
    35.             Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
    36.             foreach (var collider in colliders)
    37.             {
    38.                 collider.enabled = true;
    39.             }
    40.         }
    41.     }
    42.  
    43.  
    44. void OnEnable()
    45.     {
    46.         Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
    47.         foreach (var collider in colliders)
    48.         {
    49.             collider.enabled = true;
    50.         }
    51.         //transform.SetParent(null);
    52.     }
     
    Last edited: May 10, 2020
  4. elliot_eserin

    elliot_eserin

    Joined:
    Jan 5, 2020
    Posts:
    38
    Im not completely sure what your end goal is. But if you look at your code, you are setting your colliders enabled to false after leaving a moving platform. If you do this, the gameobject will no longer detect collisions and will pass through everything and won't trigger any onCollider checks. Which is probably why you are going through the ground? That would be my guess anyway.

    I also apologise for the long reply time...
     
  5. elliot_eserin

    elliot_eserin

    Joined:
    Jan 5, 2020
    Posts:
    38
    You also want to look at your code and break it down more. You are basically repeating the same piece of code 5 times. If you split it off into a function that just takes a true or false parameter and then sets the collider to that value, it'll be more efficient, take up less space, look cleaner and it'll be easier to debug.