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. Dismiss Notice

Stop Script From Running?

Discussion in 'Scripting' started by JWdell, May 3, 2019.

  1. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    Hey everyone! I'm having an issue with trying to get a script to stop running after it has been executed but I can't seem to find a solution or even know if it is possible to do.

    Basically what I'm doing is when the player enters a collider with a specific tag, I start a small script with a sequence of things that happen and I turn on and off the collider of the other object so that it will restart the script basically. I don't want to do it in a (while) since I don't want the script always running. When I exit that collider, I want the script sequence to completely stop running. This however doesn't happen. When I enter the collider trigger again, it just starts the script sequence all over but still also runs the previous instance of where it left off last when I exited the collider trigger. What I need is to complete stop that scripts sequence to not continue where it left off once I reenter the collider trigger. What currently happens is if you keep exiting and reentering the collider trigger then I have multple instances of this below script starting running from the beginning as well as previous instances continuing where I left off. I don't want them to continue where it left off when I exit the collider trigger and reenter it.

    function OnTriggerEnter(other : Collider)
    {
    if (other.CompareTag("TriggerPlayerCollider"))
    {
    yield WaitForSeconds(5);
    ThisCollider.SetActive(true);
    yield WaitForSeconds(.5);
    ThisCollider.SetActive(false);
    TheOtherCollider.GetComponent.<Collider>().enabled = false;
    yield WaitForSeconds(.5);
    TheOtherCollider.GetComponent.<Collider>().enabled = true;
    }
    }
     
  2. deynekos1488

    deynekos1488

    Joined:
    Mar 20, 2019
    Posts:
    14
    Try this
    Code (CSharp):
    1. private bool isExecuted = false;
    2.  
    3. if (other.CompareTag("TriggerPlayerCollider") && !isExecuted)
    4. {
    5.      yield WaitForSeconds(5);
    6.      ThisCollider.SetActive(true);
    7.      yield WaitForSeconds(.5);
    8.      ThisCollider.SetActive(false);
    9.      TheOtherCollider.GetComponent.<Collider>().enabled = false;
    10.      yield WaitForSeconds(.5);
    11.      TheOtherCollider.GetComponent.<Collider>().enabled = true;
    12.      isExecuted = true;
    13. }
    14.  
     
  3. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    EDIT: OK this wasn't working how I was intending so I made a bit of modifications and now it works as intended. When I first collide with the trigger, I start the script sequence but immediately turn the collision off and set the variable to true so that if you exit the trigger and reenter during the script sequence, nothing happens again until I turn the collision back on then set the variable back to false so that it can be retriggered from the beginning and no way to restart it during the middle of the sequence. Thanks for the help!

    Code (CSharp):
    1. private var isExecuted : boolean = false;
    2.  
    3. function OnTriggerEnter(other : Collider)
    4. {
    5. if (other.CompareTag("TriggerPlayerCollider") && !isExecuted)
    6. {
    7.      yield WaitForSeconds(.1);
    8.      isExecuted = true;
    9.      TheOtherCollider.GetComponent.<Collider>().enabled = false;
    10.      yield WaitForSeconds(5);
    11.      ThisCollider.SetActive(true);
    12.      yield WaitForSeconds(.5);
    13.      ThisCollider.SetActive(false);
    14.      yield WaitForSeconds(.5);
    15.      TheOtherCollider.GetComponent.<Collider>().enabled = true;
    16.      isExecuted = false;
    17. }
    18.  
     
    Last edited: May 4, 2019