Callback of raycast not casting on certain objects anymore? Alternative to OnTriggerEnter/Exit2D

Hello.

In my project I want to implement a wall hide/resize mechanic which I already did, but it is very error prone. CUrrently it’s working on Triggers. But once player changes goes up or down and it’s still in collision they won’t run again.

I want to use Pyshics2D.Raycast on player update so it can activate building hiding/resizing, and that works well. Thing is I can’t return the building to default state since the Raycast is no longer there and I am losing reference to this building.

Is there any method for when Raycast loses contact with this building?

Example of how it works in video below.

https://clipchamp.com/watch/gtNXD87jMfp

Code I am using right now with OnTriggerExit/Enter

using System.Collections.Generic; 
using UnityEngine; 

public class HideOverPlayerComponents : MonoBehaviour 
{ 
public delegate void OnCollisionToHideFloor(bool value, GameObject gameObject, float playerZ); 
public static event OnCollisionToHideFloor CollisiionToHideFloor; 

private void OnTriggerEnter2D(Collider2D collider) 
{ 
if (collider.gameObject.CompareTag("BuildingHider")) 
{ 
if (transform.position.z >= collider.transform.position.z) 
{ 
// Debug.Log("Collider Enter: " + collider.transform.parent.transform.parent.name + " : " + collider.transform.parent.name, collider.gameObject); 
BuildingHider buildingHider = collider.transform.root.GetComponent<BuildingHider>(); 
CollisiionToHideFloor?.Invoke(false, collider.transform.parent.transform.parent.gameObject, transform.position.z); 
} 
} 
} 

private void OnTriggerExit2D(Collider2D collider) 
{ 
if (collider.gameObject.CompareTag("BuildingHider")) 
{ 
if (transform.position.z >= collider.transform.position.z) 
{ 
// Debug.Log("Collider Exit: " + collider.transform.parent.transform.parent.name + " : " + collider.transform.parent.name, collider.gameObject); 
BuildingHider buildingHider = collider.transform.root.GetComponent<BuildingHider>(); 
if (buildingHider.floatList.Contains(collider.transform.position.z)) 
{ 
CollisiionToHideFloor?.Invoke(true, collider.transform.parent.transform.parent.gameObject, transform.position.z); 
} 
} 
} 
} 
}```

Usually you keep track of the things you have turned off so you can turn them back on again.

ALSO… nobody should need to use code like this:

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting it all one one line DOES NOT make it faster. That’s not how compiled code works.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

Thank you. So just simply keep in the for Example in the List or something like that?

Thank you, for pointing out this thing about dots.