StartCoroutine in OnTriggerEnter not firing my IEnumerator function

Hi, I browsed the forum a lot to get a clear answer but somehow I ended up tossing onto a wall. I check for an orb and platform collide which enters a trigger and scores up or down. The scoring is working fine but I want the orb to be disappeared after 1 second. I thought I wrote the code but I don’t understand why it is not working. It doesn’t go into the IEnumerator function. I put a breakpoint there to debug it; when I click to step into, it just passes it like it is doing noting too. I created a test function for it and tried to start coroutine again but it still not printing anything and not going into the function when debugging. What am I doing wrong here, what should I do to make the orb disappear after a 1-second pass?

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

public class OrbCatch : MonoBehaviour
{
    public GameObject scoreDisplay;
    public static int playerScore = 100;
    public AudioSource orbCatchFX;
    public AudioSource orbCatchSuperFX;
    public AudioSource orbCatchWrongFX;

    // Start is called before the first frame update
    private void OnTriggerEnter(Collider other)
    {
        System.Console.WriteLine(other.tag);
        switch (other.tag)
        {
            case "GreenCube":
                orbCatchFX.Play();
                playerScore = playerScore + 10;
                scoreDisplay.GetComponent<Text>().text = "Score: " + playerScore;
                break;
            case "BlueCube":
                orbCatchSuperFX.Play();
                playerScore = playerScore + 100;
                scoreDisplay.GetComponent<Text>().text = "Score: " + playerScore;
                break;
            case "RedCube":
                orbCatchWrongFX.Play();
                playerScore = playerScore - 50;
                scoreDisplay.GetComponent<Text>().text = "Score: " + playerScore;
                break;
            default:
                break;
        }
        StartCoroutine(ResetOrb(other.gameObject));
        StartCoroutine(myTest());
    }

    IEnumerator ResetOrb(GameObject orb)
    {
        yield return new WaitForSeconds(1f);
        orb.SetActive(false);
    }
    IEnumerator myTest()
    {
        yield return new WaitForSeconds(1f);
        print("test");
    }
}

I don’t think system console will print anything under Unity. Use Debug.Log() instead. Old habits die hard, I know. :slight_smile:

To help gain more insight into your 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 are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Print method is printing pretty well normally on unity console instead vs console. Like if I put print(“test”) in update function, it prints test to unity console test without problem but not in my situation on above code as I debugged it is not even going into the IEnumerator myTest().

I now tried start a new coroutine in update function in the same script and it works properly. It just not starting in ontriggerenter.

public void Update()
{
     StartCoroutine(myTest());
}

I tried in a different class, it calls IEnumerator from StartCoroutine function as intended but still not working in that piece of code.