In my scene I have a ball and I want to store a reference to the first Collision it encounters. I have two GameObjects in the scene, each with a unique tag. With the following code I expect the variable to only be updated on the first collision, however it is being updated on every collision to the most recent collision.
public class GolfBallCollision : MonoBehaviour {
public Collision contactFirst = null;
public void OnCollisionEnter(Collision collision)
{
CheckFirst(collision);
Debug.LogWarning(contactFirst.gameObject.tag);
}
public void CheckFirst(Collision hit)
{
if (contactFirst == null)
{
contactFirst = hit;
}
}
}
What happens when I try this is the the log prints the tag of whichever GameObject it collided with LAST instead of just the first one every time. Please let me know if any additional information is needed, thanks.