The question is answered, right answer was accepted
Why isn't my integer changing when onTriggerEnter is called?
Hi,
I'm creating a game where you cross two lanes of traffic and I'm trying to get it so when my player collides with a certain car it tells me which car it was. I've tried making it so if it hits carA it results in the variable carCollision changing to 1 etc. But when the player collides with all cars in game it just produces '0'. What am I doing wrong?
public class CollisionCar : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
//Debug.Log(gameObject.name + "has collidied with" + col.gameObject.name);
if (col.gameObject.tag == "carA")
{
mainManager.carCollision = 1;
}
if (col.gameObject.tag == "carB")
{
mainManager.carCollision = 2;
}
if (col.gameObject.tag == "carC")
{
mainManager.carCollision = 3;
}
if (col.gameObject.tag == "carD")
{
mainManager.carCollision = 4;
}
Debug.Log(mainManager.carCollision);
}
}
That would suggest that the gameobject with which you are colliding is tagged something other than carA, carB, carC, or carD. Why don't you add:
Debug.Log(col.gameObject.tag);
?
Did you make sure that your objects are tagged correctly?
Answer by Staffordwhat · Nov 22, 2016 at 03:44 PM
Thank you so much, I realized I had attached the script to the cars instead of the player.