Hey guys, I’m really new to Unity and I’m having some issues with tags. How can I check if my gameObject has collided with 2 other gameObjects that have 2 different tags?
Example of what I’m doing :
public GameObject myObject;
public GameObject myCloneObject;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Hydrogen") && other.gameObject.CompareTag("Helium2"))
{
if (myCloneObject == null)
{
Vector3 offset = new Vector3(-0.5f, 0, 0);
myCloneObject = Instantiate(myObject, transform.position + offset, transform.rotation);
}
}
}
The app is an Augmented Reality one. I detect 3 markers each with different gameObjects. I’ve set up my BoxCollider and Rigidbody on each of them and made them large enough so they can collide. The gameObject in the middle has a BoxCollider that is set to “isTrigger”. What i want to happen is that when my 2 other gameObjects collide with it, i want to instantiate another gameObject and hide the current ones(this is done by another script and it works just fine).
My guess is that: if (other.CompareTag(“Hydrogen”) && other.gameObject.CompareTag(“Helium2”)) this line of code is not working with the logical operator && , because when i’m switching it to a logical operator || everything works fine and my gameObject gets instantiated but only when one of the tags just hit not both.
You never have to guess. This is engineering. It is highly unlikely that the && operator stopped working!
Instead of guessing, find out what the tags are by using Debug.Log() at the start of your function, then you can reason about what is happening.
To help gain more insight into any problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run? what order does it run in?
- what are the values of the variables involved? Are they initialized? Are the values reasonable?
- are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
https://discussions.unity.com/t/839300/3
Hey,
The issue is that you’re asking if each individual collision object has both tags, which is impossible. “Collider other” can only be one object at a time.
There are probably better ways of doing this, but here’s what I came up with off the top of my head:
bool collidedWithHydrogen = false;
bool collidedWithHelium2 = false;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Hydrogen"))
{
collidedWithHydrogen = true;
}
else if (other.gameObject.CompareTag("Helium2"))
{
collidedWithHelium2 = true;
}
if (collidedWithHydrogen && collidedWithHelium2)
{
if (myCloneObject == null)
{
Vector3 offset = new Vector3(-0.5f, 0, 0);
myCloneObject = Instantiate(myObject, transform.position + offset, transform.rotation);
collidedWithHydrogen = false;
collidedWithHelium2 = false;
}
}
}
This uses two bools to check if the necessary objects have been collided with. If you want to check if you’re colliding with both objects on the same frame, this won’t work, but maybe it will give you an idea of how to proceed.
Thanks, to both of you. Managed to get it fixed.
1 Like