I want to save the object which triggered as a variable and afterwards destroy it by pushing a key but I couldn't figure out how to save it as a variable.
private void OnTriggerEnter(Collider other)
{
if (other.gameObject)
_canHit = true;
}
Edit: (Added the whole script to make it more understandable)
//GameObject variable
public GameObject collidedWith;
//store the collided GameObject
private void OnTriggerEnter(Collider other)
{
collidedWith = other.gameObject;
}
private bool _canHit;
//if can hit true make it false and do the task
private void Update()
{
if (_canHit)
{
if (Input.GetKeyDown(KeyCode.A) && collidedWith != null)
{
Destroy(collidedWith);
_canHit = false;
Debug.Log("Left");
}
}
}
//set _canHit true if object enters trigger
private void OnTriggerEnter(Collider other)
{
if (other.attachedRigidbody)
_canHit = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject)
trigObj = other.gameObject()
}
//set _canHit false if object enters trigger
private void OnTriggerExit(Collider other)
{
if (other.attachedRigidbody)
_canHit = false;
}