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

Bug Stopping Coroutine from same script

Discussion in 'Scripting' started by RottenH20, Sep 14, 2022.

  1. RottenH20

    RottenH20

    Joined:
    Sep 6, 2018
    Posts:
    6
    I have a problem with stopping a Coroutine from the same Coroutine from another script. For certain reasons I have a setup where the player enters a Collider then a "end game" Coroutine triggers. This Coroutine enables then disables gravity for the character. This coroutine is on both Colliders as the same script just on both colliders.

    For context the "zones" the player enters will always be entered at the same time (Same frame). Both the "zones" or colliders are called "Bounds 1". There are multiple of these (about 30) per level and they are all called "Bounds 1". They are all tagged with "Bounds". Currently I have a setup (that is horrible and most likely a better way to do it), where the player enters both the colliders and the script starts both of these Coroutines in each script respectively.

    The problem is I only need one of these Coroutines to happen. Which one does not matter, I just need only one of them to work. I'm not sure how to do that whenever they start at the same time however.

    The current setup is a randomNumber between -100000 and 100000 (Change for same number is small). Then the scripts set their names to their numbers. After which the coroutine waits for both scripts to have changed their names (WaitForSeconds 0.1f). Then it destroys whichever one has the lower of the two values.

    The script "freezes" the block in place and never enables gravity for the player. I have tried Destroy, SetActive(false), and Disabling the script on the Bounds gameObject. However the player block always never enables gravity.

    There is probably a solution, however I don't think I am advanced enough yet to understand what it could be. Thank you in advance for helping!

    Code (CSharp):
    1. IEnumerator initiateFall() // Normal Fall
    2.     {
    3.         int randomNumber = Random.Range(-100000, 100000);
    4.         this.gameObject.tag = ("ActiveBounds");
    5.         this.gameObject.name = ("ActiveBounds " + randomNumber);
    6.         yield return new WaitForSeconds(0.1f);
    7.         GameObject[] BoundsArray = GameObject.FindGameObjectsWithTag("Bounds");
    8.         foreach (GameObject go in BoundsArray)
    9.         {
    10.             go.SetActive(false);
    11.         }
    12.         yield return new WaitForSeconds(0.1f);
    13.         GameObject[] ActiveBoundsArray = GameObject.FindGameObjectsWithTag("ActiveBounds");
    14.         if (ActiveBoundsArray.Length > 1)
    15.         {
    16.             if (int.Parse(ActiveBoundsArray[0].name.Substring(12)) > int.Parse(ActiveBoundsArray[1].name.Substring(12))) // Parse convert String to int
    17.                 Destroy(ActiveBoundsArray[1]);
    18.             else
    19.                 Destroy(ActiveBoundsArray[0]);
    20.  
    21.             Debug.Log(ActiveBoundsArray[1].name.Substring(12));
    22.             Debug.Log(ActiveBoundsArray[0].name.Substring(12));
    23.         }
    24.         CanvasHandlerLevel.flipInput();
    25.         yield return new WaitForSeconds(0.3f); // Wait for character to stop horizontally moving
    26.         if (SceneManager.GetActiveScene().name == "Level2") // Level2 is outlier because its alligned y value is incorrect and this is temp fix
    27.             Outliner.transform.position = new Vector3(this.transform.position.x, 0, this.transform.position.z);
    28.         else
    29.             Outliner.transform.position = new Vector3(this.transform.position.x, 1, this.transform.position.z);
    30.         StartCoroutine(blinkOutlier()); // Enables and disables outliner over fixed period
    31.         complete = false;
    32.         while (!complete) // Keep delaying script until character is in place
    33.             yield return new WaitForSeconds(0.1f);
    34.  
    35.         yield return new WaitForSeconds(1f); // Pause for player to know where outliner is blinking
    36.         rbPlayerBlock.isKinematic = false;
    37.         rbPlayerBlock.useGravity = true;
    38.         yield return new WaitForSeconds(3); // We allow player to fall free for 3 seconds (until off screen) then stop it to save data
    39.         CanvasHandlerLevel.playerDied();
    40.         rbPlayerBlock.isKinematic = true;
    41.         rbPlayerBlock.useGravity = false;
    42.     }
     
  2. RottenH20

    RottenH20

    Joined:
    Sep 6, 2018
    Posts:
    6
    Sorry here is the Update Method called for the loop (Inside same class)


    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (!complete)
    4.         {
    5.             t += Time.deltaTime / timeToReachTarget; // Find time till completion
    6.             Vector3 relativePos = this.transform.position + playerBlock.transform.position; // Find where player should go
    7.             playerBlock.transform.rotation = Quaternion.Lerp(playerBlock.transform.rotation, Quaternion.Euler(0, 0, 0), t); // Start rotating player correctly
    8.             playerBlock.transform.position = Vector3.Lerp(playerBlock.transform.position, new Vector3(this.transform.position.x, this.transform.position.y + 2f, this.transform.position.z), t); // Move to spot over certain time frame (t)
    9.             if (playerBlock.transform.rotation == Quaternion.Euler(0, 0, 0)) // Once player rotation is 0, 0, 0 we continue script
    10.                 complete = true;
    11.         }
    12.     }