I have two similar game objects on my scene that act as player protection areas, meaning that objects that is within these areas is protected.
The two areas have rigidbody2d and collider2d attached and the player objects have collider2d attached.
I have a script attached to the areas with “Void OnTriggerStay2D(Collider2D col) {”.
This is triggered when i move a player object into the player protection areas and i am also able to identify these objects using the “col.tag” parameter.
However, is it possible to also identify the player protection area GameObjects in the same script?
The object you’re looking for will be the object that the OnTriggerStay is executing against - so you can reference it simply as “gameObject” in code. The other object (in this case the Player) is the collider that you’ve called “col” (col.gameObject for it’s GameObject).
private void OnTriggerStay(Collider col)
{
Debug.Log(string.Format("{0} - I'm the player area that has been collided with.", gameObject.name));
Debug.Log(string.Format("{0} - I'm the object that has entered the area.", col.gameObject.name));
}
EDIT: Just for completeness, you might not want to use OnTriggerStay in your case. I would think that OnTriggerEnter you’d set a player flag like IsProtected = true, and then OnTriggerExit, IsProtected = false.