- Home /
Question by
uzairbsqptech · Aug 26, 2020 at 10:14 AM ·
unity 5gameobjectcolliderplayer
I am getting player tag not the other object that i want to destroy.
i place 7 Timer object in map and i have ninja who collect this timers; i assign timer a tag "ExtraTime" and ninja has a "Player" tag.
but when i hit with timer all object destroy at once. i need to destroy the only object that i collide with
this is my code
void OnCollisionEnter(Collision collision)
{
Timer = GameObject.FindGameObjectWithTag("ExtraTime");
ResearchAndDevelopment.BabyCatchTimer.ElappsedSeconds += 120;
Destroy(Timer);
}
Comment
Answer by MatthewOCallaghan · Aug 26, 2020 at 09:02 PM
I assume that script is on the player, in which case the Collision object passed into the OnCollisionEnter method contains information on which timer was collected. Something like this should just destroy the one timer:
void OnCollisionEnter(Collision collision)
{
// If we hit a timer
if (collision.gameObject.tag == "ExtraTime")
{
// Add time here
// Destroy this specific timer
Destroy(collision.gameObject);
}
}
At the moment, you are just searching for a single timer each time there is a collision, so no guarantee it is the timer you want.