How can I disable collision?

I have an object which, upon meeting certain conditions, will “disappear” as far as the player can tell, and later reappear. Getting it to not be visible is easy. However, the player is capable of standing on said object when it is visible. Thus, this object has a collision. (It also has a rigidbody because it moves.) So in addition to making the object invisible, I need the object to stop colliding with the player. (Or really any object at all.)

I am having difficulty making the object stop colliding with the player.
I thought I could simply disable it, like “myCollison.enabled = false;”. But I run into two errors.

First of all, I have the error “Type UnityEngine.Collision2D' does not contain a definition for enabled’ and no extension method enabled' of type UnityEngine.Collision2D’ could be found” which means I can’t simply disable the collsion with that flag.

Secondly, I am not properly getting the collision to use as a variable within my script. I tried “myCol = GetComponent();” but I get the error “The type UnityEngine.Collision2D' must be convertible to UnityEngine.Component’ in order to use it as parameter T' in the generic type or method UnityEngine.Component.GetComponent()'” so I’m going about that the wrong way too.

So I have two questions.

  1. How do I disable (and re-enable) the collision of an object so it can collide and not collide at a whim?

  2. How do I access an object’s own collision in the script?

Collider, not collision

You can have a layer called “NoCollide” and a layer for the player called “Player”, and in Edit > Project Settings > Physics/Physics2D (Depending on which you are using) in the Layer Collision Matrix uncheck the Player/NoCollide layer collision toggle. This will keep all objects in the layer “Player” from colliding with any objects in the layer “NoCollide” while still allowing the object to collide with all other layers that are toggle on (e.g. the terrain so it doesn’t fall through).

As for your questions I only know how to disable the collider: Unity - Scripting API: Collider.enabled
But using the layer collision matrix sounds like your best bet.