- Home /
Trigger works but i the statement code is not doing what it should.
I have this game i am working on. And i am having a Player prefab that is the one the user controlling. I have coins that i want the Player to collect them when he is passing by them. using UnityEngine; using System.Collections;
public class PickUpCoin : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("collision name = " + other.gameObject.name);// i track if the collision values are correct.And they are.
if (other.gameObject.CompareTag("Player")) {
Destroy(gameObject);
}
}
}
The thing is that the Player tag is Player and when i am passing by it the debug correctly says that they actually collided. The coin has "Is Triggered" checked and also a circleColider2D. What basically i believe it's happening is that my object is not destroyed. I am confused. I watched all the tutorials in the site that say about collecting coins and destroying the object. i cant trace the error here. I would greatly appreciate your suggestions. Thank you :D
You could start by Debug.Logging the tag ins$$anonymous$$d of the name of the object to be sure the tag matches.
Then you could add another Debug.Log inside the if-block to see if you enter it and are just destroying a different object than tou think (for example)
Yes - I agree with @Nose$$anonymous$$ills. The fact that the other gameObject is called "Player" does not mean it has the tag "Player".
Answer by Stiliatis Togrou · Jun 24, 2016 at 04:11 PM
It seems you were right. I the problem was that the tag wasn't Player. Thank for the help and sorry for the noob answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083542im_/https://answers.unity.com/themes/thub/images/avi.jpg)