Problem while using several tags in a OnTriggerEnter (Collider other)
I have a question in regards to the tag system. I am doing the Create with Code tutorial and I am trying to expand a little bit the second project (it's a game in which you shoot food to animals to feed them and make them disappear). In my version, they have different tastes so you need to cycle through different types of food ammo and your projectiles should only be effective if you choose the correct one. Right now I am passing a "foodIndex" variable from the animal's prefab that indicates which food he wants (0 for pizza, 1 for steak). Unfortunately, my OnTriggerEnter method, the tags are not working correctly. Only my pizza ammo works (foodType 0), and it works both when the animals ask for pizza or steak, and the steak never works. If I exchange the tag "pizza" from the second if condition to "steak" then the logic is reversed and only the steak works. The Debug.Log shows me that the foodType variable is been correctly updated by the foodIndex, so I'm lost about what the issue could be. I've been programming in C# for just around 3 weeks, so maybe I am missing some fundamentals about how the tag system actually works. I appreciate any feedback and criticism. Here is the relevant portion of my code:
public void UpdateIndex(int foodIndex) { foodType = foodIndex; if (foodType == 0) { Debug.Log("foodType:" + foodType); } if (foodType == 1) { Debug.Log("foodType:" + foodType); } } void OnTriggerEnter(Collider other) { //Check if the other tag was the Player, if it was remove a life if (other.CompareTag("Player")) { gameManager.AddLives(-1); Destroy(gameObject); } //if foodtype is 0 (pizza), the other tag is "Animal", and I am "pizza" then add points to the score if (foodType == 0 && other.CompareTag("Animal") && gameObject.tag == "pizza") { other.GetComponent<AnimalHunger>().FeedAnimal(1); Destroy(gameObject); Debug.Log("pizza hit!"); } //if foodtype is 1 (steak), the other tag is "Animal", and I am "steak" then add points to the score if (foodType == 1 && other.CompareTag("Animal") && gameObject.tag == "steak") { other.GetComponent<AnimalHunger>().FeedAnimal(1); Destroy(gameObject); Debug.Log("steak hit!"); } }
Thanks in advance.