OnTriggerEnter2D seems to be changing gameObject tag to Untagged.

I have a bullet gameObject with the tag “Bullet” and a target gameObject.
In the bullet’s script I have the following included in the Start() method:

Debug.Log( gameObject.tag + “exists.”);

In the target’s script I have the following in the OnTriggerEnter2D(Collider2D other) method:

Debug.Log(other.gameObject.tag + “has hit the target”.

I have it scripted that the bullet gameObject will collide with the target gameObject. I expected the following output from Debug.Log:
Bullet exists.
Bullet has hit the target.

The output I actually got was:
Bullet exists.
Untagged has hit the target.

I can’t figure this out. Is it a bug? I’m using Unity version 2020.3.30f1.,I’m using Unity version 2020.3.30f1.

Don’t forget that gameObject and other.gameObject are completely different objects.

What tag does the object that it’s hitting have?

the bullet object has Bullet for its tag.

Right, but other.gameObject is not the bullet. It’s the other object. The object that the bullet hits.

the bullet object does not have OnTriggerEnter2D() in it’s script. The target object has the OnTriggerEnter2D() in its script. The target object is tagged with Target.

Update: I made some changes and made it so my bullet object could hit something else. That object’s tag comes up as expected. I believe something happened to the target’s prefab (because the issue happens to all instances of the prefab).

Oh, I see now. Your first post is showing two separate scripts. You should use CODE tags whenever you post code. It makes it easier to read.

Try adding debug.log(gameObject.name) and (other.gameObject.name) where you are logging the tags. Make sure that the game objects that you are looking are what you think they are. I’m thinking that the “untagged” object may not be the bullet at all.

I will try that.

So you know, physics has nothing to do with tags, so stating that it’s somehow changing it isn’t correct.

Physics uses layers, not tags and it only reads them, never changes them. Tags, like layers are assigned to GameObjects which isn’t controlled by physics. We see this a lot, suggesting it’s a bug when it’s simply a misunderstanding on how things work.

@kdgalla Thanks! Displaying the object names helped me figure out what the issue was.

@MelvMay Your response didn’t help at all and I interpreted it as rude.

My issue was simple. The collider is on a child object, not the parent. I set the parent object’s tag but not the child. That’s why I wasn’t getting the result I had expected. I gave the child object the proper tag and my code worked as expected.

For the record/transparency here, I just sent an apology (which was gracefully accepted!) to armaghedron here because I didn’t realise how rude what I said “sounded”. :slight_smile: