Search code examples
c#unity-game-engine

OnTriggerEnter not working 100% of the time


I'm getting frustrated with the trigger event not working one hundred percent of the time. The way I'm using this is for my "pizza system" that I have been working on for a project to test my coding knowledge for a personal milestone. But now with this problem, I'm finally getting annoyed so I'm trying to see if anyone could help :)

Their are three scripts attached to this system, one that controls the ingredients and stuff, the oven cooking it (which Isn't fully finished) and the final order destination, which is the problem I'm having.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class orderDropOff : MonoBehaviour
{
    public pizzaManager pManager;
    [SerializeField]
    public TMP_Text pizzaPick;
    public TMP_Text pizzaComplete;

    void Start()
    {
        pizzaComplete.enabled = false;
        pickingPizza();
    }

    void pickingPizza()
    {
        int PizzaIndex = Random.Range(1, 6);

        if (PizzaIndex == 1)
        {
            Debug.Log("1");
            pizzaPick.text = "Pepperoni Pizza";
        }
        if (PizzaIndex == 2)
        {
            Debug.Log("2");
            pizzaPick.text = "Veggie Pizza";
        }
        if (PizzaIndex == 3)
        {
            Debug.Log("3");
            pizzaPick.text = "Boujee Pizza";
        }
        if (PizzaIndex == 4)
        {
            Debug.Log("4");
            pizzaPick.text = "Pepperoni & Pepper";
        }
        if (PizzaIndex == 5)
        {
            Debug.Log("5");
            pizzaPick.text = "Litterbox Pizza";
        }
        if (PizzaIndex == 6)
        {
            Debug.Log("6");
            pizzaPick.text = "Everything Pizza";
        }
    }


    void OnTriggerEnter(Collider collision)
    {
        int PizzaIndex = Random.Range(1, 6);
        if ((collision.tag == "Player") && pManager.pepperoniPizza && pManager.pizzaIsFinished && PizzaIndex == 1)
        {
            pizzaComplete.enabled = true;
            FindObjectOfType<AudioManager>().Play("youWon!");
            pManager.Reset();
            pickingPizza();
            StartCoroutine(yasAnimation());

            //get batteries
        }

        if ((collision.tag == "Player") && pManager.veggiePizza && pManager.pizzaIsFinished && PizzaIndex == 2)
        {
            pizzaComplete.enabled = true;
            FindObjectOfType<AudioManager>().Play("youWon!");
            pManager.Reset();
            pickingPizza();
            StartCoroutine(yasAnimation());

            //get batteries
        }
        
        if ((collision.tag == "Player") && pManager.boujeePizza && pManager.pizzaIsFinished && PizzaIndex == 3)
        {
            pizzaComplete.enabled = true;
            FindObjectOfType<AudioManager>().Play("youWon!");
            pManager.Reset();
            pickingPizza();
            StartCoroutine(yasAnimation());

            //get batteries
        }
        if ((collision.tag == "Player") && pManager.pP && pManager.pizzaIsFinished && PizzaIndex == 4)
        {
            pizzaComplete.enabled = true;
            FindObjectOfType<AudioManager>().Play("youWon!");
            pManager.Reset();
            pickingPizza();
            StartCoroutine(yasAnimation());

            //get batteries
        }
        if ((collision.tag == "Player") && pManager.litterboxPizza && pManager.pizzaIsFinished && PizzaIndex == 5)
        {
            pizzaComplete.enabled = true;
            FindObjectOfType<AudioManager>().Play("youWon!");
            pManager.Reset();
            pickingPizza();
            StartCoroutine(yasAnimation());

            //get batteries
        }
        if ((collision.tag == "Player") && pManager.everythingPizza && pManager.pizzaIsFinished && PizzaIndex == 6)
        {
            pizzaComplete.enabled = true;
            FindObjectOfType<AudioManager>().Play("youWon!");
            pManager.Reset();
            pickingPizza();
            StartCoroutine(yasAnimation());

            //get batteries
        }
    }

    IEnumerator yasAnimation()
    {
        pizzaComplete.enabled = true;
        yield return new WaitForSeconds(1);
        pizzaComplete.enabled = false;
    }
}

When I walk it, either it: works fine, it doesn't work at all, works when the first pizza is done, etc. I tried to have it put into a update method trying to see it wasn't just not updating everything frame but doesn't seem to work.

If anyone has every ran into this issue or anything similar please respond! Or need anymore information please get back to me I'm here to help too :)


Solution

  • Your problem may comes from the random being run twice. Heres some clean up:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    
    public class orderDropOff : MonoBehaviour
    {
        public pizzaManager pManager;
        [SerializeField]
        public TMP_Text pizzaPick;
        public TMP_Text pizzaComplete;
    
        private int pizzaIndex = 1;
    
        void Start()
        {
            pizzaComplete.enabled = false;
            pickingPizza();
        }
    
        void pickingPizza()
        {
            pizzaIndex = Random.Range(1, 6);
    
            string pizzaText = "";
            switch (pizzaIndex)
            {
                1:
                    pizzaText = "Pepperoni Pizza";
                    break;
                2:
                    pizzaText = "Veggie Pizza";
                    break;
                3:
                    pizzaText = "Boujee Pizza";
                    break;
                4:
                    pizzaText = "Pepperoni & Pepper";
                    break;
                5:
                    pizzaText = "Litterbox Pizza";
                    break;
                6:
                    pizzaText = "Everything Pizza";
                    break;    
                default:
            }
    
            pizzaPick.text = pizzaIndex;
    
        }
    
    
        void OnTriggerEnter(Collider collision)
        {
            bool playerCollide = collision.tag == "Player";
    
            bool isCorrectPizza = false;
    
            if(playerCollide){
                isCorrectPizza |= pManager.pepperoniPizza && pizzaIndex == 1;
                isCorrectPizza |= pManager.veggiePizza && pizzaIndex == 2;
                isCorrectPizza |= pManager.boujeePizza && pizzaIndex == 3;
                isCorrectPizza |= pManager.pP && pizzaIndex == 4;
                isCorrectPizza |= pManager.litterboxPizza && pizzaIndex == 5;
                isCorrectPizza |= pManager.everythingPizza && pizzaIndex == 6;
    
                if(pManager.pizzaIsFinished){
                    pizzaComplete.enabled = true;
                    FindObjectOfType<AudioManager>().Play("youWon!");
                    pManager.Reset();
                    pickingPizza();
                    StartCoroutine(yasAnimation());
    
                    //get batteries
                }
            }
        }
    
        IEnumerator yasAnimation()
        {
            pizzaComplete.enabled = true;
            yield return new WaitForSeconds(1);
            pizzaComplete.enabled = false;
        }
    }
    

    if this does not help you should edit your post to tell more about the desired behaviour