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

Question Stop the script

Discussion in 'Scripting' started by gleblola389, Sep 18, 2023.

  1. gleblola389

    gleblola389

    Joined:
    Sep 13, 2023
    Posts:
    6
    Hey guys, how can I full stop script after 1 sec?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomSpawner : MonoBehaviour
    6. {
    7.     public Direction direction;
    8.  
    9.     public enum Direction
    10.     {
    11.         Top,
    12.         Bottom,
    13.         Left,
    14.         Right,
    15.         None
    16.     }
    17.  
    18.     private RoomVariants variants;
    19.     private int rand;
    20.     private bool spawned = false;
    21.     private float waitTime = 3f;
    22.  
    23.     private void Start()
    24.     {
    25.         variants = GameObject.FindGameObjectWithTag("Rooms").GetComponent<RoomVariants>();
    26.         Destroy(gameObject, waitTime);
    27.         Invoke("Spawn", 0.2f);
    28.         StartCoroutine (walera());
    29.     }
    30.     public void Spawn()
    31.     {
    32.         if(!spawned)
    33.         {
    34.             if(direction == Direction.Top)
    35.             {
    36.                 rand = Random.Range(0, variants.topRooms.Length);
    37.                 Instantiate(variants.topRooms[rand], transform.position, variants.topRooms[rand].transform.rotation);
    38.             }
    39.             else if(direction == Direction.Bottom)
    40.             {
    41.                 rand = Random.Range(0, variants.bottomRooms.Length);
    42.                 Instantiate(variants.bottomRooms[rand], transform.position, variants.bottomRooms[rand].transform.rotation);
    43.             }
    44.             else if(direction == Direction.Right)
    45.             {
    46.                 rand = Random.Range(0, variants.rightRooms.Length);
    47.                 Instantiate(variants.rightRooms[rand], transform.position, variants.rightRooms[rand].transform.rotation);
    48.             }
    49.             else if(direction == Direction.Left)
    50.             {
    51.                 rand = Random.Range(0, variants.leftRooms.Length);
    52.                 Instantiate(variants.leftRooms[rand], transform.position, variants.leftRooms[rand].transform.rotation);
    53.             }
    54.                 spawned = true;
    55.         }
    56.     }
    57.     private void OnTriggerStay2D(Collider2D other)
    58.     {
    59.         if(other.CompareTag("RoomPoint") && other.GetComponent<RoomSpawner>().spawned)
    60.         {
    61.             Destroy(gameObject);
    62.         }
    63.     }
    64.     IEnumerator walera()
    65.     {
    66.         yield return new WaitForSeconds(1);
    67.         // here I want to stop it
    68.     }
    69. }
     
  2. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
  3. gleblola389

    gleblola389

    Joined:
    Sep 13, 2023
    Posts:
    6
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    You're criticising but it would help if you specified what "full stop the script" actually meant. That coroutine will stop at the point you're asking. We can guess that you mean stop spawning but it's a guess only and maybe wrong. You're also showing a collision callback and it's unclear what relevance this is to the issue.

    Please specify exactly what you want. If you want to stop the coroutine then providing you a link to the docs that show you how is relevant?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Your code will already stop running at line 67. It's completely unclear what your intention here is.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,589
    Maybe OP just wants to .enable=false the script, or to SetActive(false) the entire gameobject.
    Instructions unclear, <insert meme about a certain male bodypart being stuck in a toaster>.
     
    Kurt-Dekker and Bunny83 like this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Right, but his script actually does nothing after 1 sec has passed. He only called his "Spawn" method once with a 0.2 sec delay. The only thing that is still relevant after that is the OnTriggerStay2D callback. However that's not even affected by the enabled state of the script AFAIR. Also all that callback does is destroying the object this script is on. The only way to completely "stop" a script is to simply destroy the script instance itself. So

    Code (CSharp):
    1. Destroy(this);
    Though for that you don't need a coroutine as you can call Destroy also with a delay argument in Start. So the whole design and purpose of the script is unclear, at least to me.
     
    Kurt-Dekker and Yoreki like this.