Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

StartCoroutine in OnTriggerEnter not firing my IEnumerator function

Discussion in 'Scripting' started by Zyrecon, Jan 20, 2021.

  1. Zyrecon

    Zyrecon

    Joined:
    Nov 13, 2020
    Posts:
    3
    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?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class OrbCatch : MonoBehaviour
    7. {
    8.     public GameObject scoreDisplay;
    9.     public static int playerScore = 100;
    10.     public AudioSource orbCatchFX;
    11.     public AudioSource orbCatchSuperFX;
    12.     public AudioSource orbCatchWrongFX;
    13.  
    14.     // Start is called before the first frame update
    15.     private void OnTriggerEnter(Collider other)
    16.     {
    17.         System.Console.WriteLine(other.tag);
    18.         switch (other.tag)
    19.         {
    20.             case "GreenCube":
    21.                 orbCatchFX.Play();
    22.                 playerScore = playerScore + 10;
    23.                 scoreDisplay.GetComponent<Text>().text = "Score: " + playerScore;
    24.                 break;
    25.             case "BlueCube":
    26.                 orbCatchSuperFX.Play();
    27.                 playerScore = playerScore + 100;
    28.                 scoreDisplay.GetComponent<Text>().text = "Score: " + playerScore;
    29.                 break;
    30.             case "RedCube":
    31.                 orbCatchWrongFX.Play();
    32.                 playerScore = playerScore - 50;
    33.                 scoreDisplay.GetComponent<Text>().text = "Score: " + playerScore;
    34.                 break;
    35.             default:
    36.                 break;
    37.         }
    38.         StartCoroutine(ResetOrb(other.gameObject));
    39.         StartCoroutine(myTest());
    40.     }
    41.  
    42.     IEnumerator ResetOrb(GameObject orb)
    43.     {
    44.         yield return new WaitForSeconds(1f);
    45.         orb.SetActive(false);
    46.     }
    47.     IEnumerator myTest()
    48.     {
    49.         yield return new WaitForSeconds(1f);
    50.         print("test");
    51.     }
    52. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    I don't think system console will print anything under Unity. Use Debug.Log() instead. Old habits die hard, I know. :)

    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.
     
  3. Zyrecon

    Zyrecon

    Joined:
    Nov 13, 2020
    Posts:
    3
    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.

    Code (CSharp):
    1. public void Update()
    2. {
    3.      StartCoroutine(myTest());
    4. }
     
  4. Zyrecon

    Zyrecon

    Joined:
    Nov 13, 2020
    Posts:
    3
    I tried in a different class, it calls IEnumerator from StartCoroutine function as intended but still not working in that piece of code.