Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Unity UI element stays active when it should be hidden

Discussion in 'Scripting' started by Miryuscal, Oct 27, 2023.

  1. Miryuscal

    Miryuscal

    Joined:
    Dec 26, 2020
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class Green : MonoBehaviour
    8. {
    9.     public Countdown objects;
    10.     public Collider ustCollider;
    11.     public TMP_Text informative;
    12.     public GameObject player;
    13.     public Camera mainCamera;
    14.  
    15.     private void Start()
    16.     {
    17.         informative.text = "+2 sec";
    18.     }
    19.  
    20.  
    21.  
    22.  
    23.     private void OnCollisionEnter(Collision collision)
    24.     {
    25.  
    26.         if (collision.collider == ustCollider && collision.gameObject.CompareTag("Player")) // Kurtun etiketini kullanabilirsiniz.
    27.         {
    28.             informative.transform.position = player.transform.position + new Vector3(0, 20, 0);
    29.             Vector3 position = informative.transform.position;
    30.             position = mainCamera.WorldToScreenPoint(position) + new Vector3(0, -1860, 0);
    31.             informative.transform.position = position;
    32.             StartCoroutine(ShowMessage());
    33.             Score.sayi++;
    34.             objects.GetComponent<Countdown>().countdownTime += 2;
    35.             Destroy(gameObject);
    36.          
    37.         }
    38.     }
    39.  
    40.    
    41.  
    42.     private IEnumerator ShowMessage()
    43.     {
    44.         informative.gameObject.SetActive(true);
    45.  
    46.         yield return new WaitForSecondsRealtime(1);
    47.  
    48.         informative.gameObject.SetActive(false);
    49.     }
    50.  
    51.  
    52.  
    53. }
    54.  
    Hey, After text shows up on screen it doesn't hidden. How can I fix it?
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,838
    Those two things don't work together. Coroutines run ON the MonoBehaviour instance you call StartCoroutine on. When you destroy the object the coroutine will die with it
     
  3. Miryuscal

    Miryuscal

    Joined:
    Dec 26, 2020
    Posts:
    3
    ı delete destroy line but still text is showing on the screen.
     
  4. Miryuscal

    Miryuscal

    Joined:
    Dec 26, 2020
    Posts:
    3
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. public class Green : MonoBehaviour
    7. {
    8.     public Countdown objects;
    9.     public Collider ustCollider;
    10.     public TMP_Text informative;
    11.     public GameObject player;
    12.     public Camera mainCamera;
    13.     private void Start()
    14.     {
    15.         informative.text = "+2 sec";
    16.     }
    17.     private void OnCollisionEnter(Collision collision)
    18.     {
    19.         if (collision.collider == ustCollider && collision.gameObject.CompareTag("Player")) // Kurtun etiketini kullanabilirsiniz.
    20.         {
    21.             informative.transform.position = player.transform.position + new Vector3(0, 20, 0);
    22.             Vector3 position = informative.transform.position;
    23.             position = mainCamera.WorldToScreenPoint(position) + new Vector3(0, -1860, 0);
    24.             informative.transform.position = position;
    25.             StartCoroutine(ShowMessage());
    26.             Score.sayi++;
    27.             objects.GetComponent<Countdown>().countdownTime += 2;
    28.             gameObject.transform.position += new Vector3(0, 0, -9f);
    29.         }
    30.     }
    31.  
    32.     private IEnumerator ShowMessage()
    33.     {
    34.         informative.gameObject.SetActive(true);
    35.        
    36.  
    37.         yield return new WaitForSecondsRealtime(1);
    38.        
    39.         informative.gameObject.SetActive(false);
    40.      
    41.     }
    42. }
    43.  
    I fix it by adding gameObject.transform.position += new Vector3(0, 0, -9f); since I another destroyer on scene. In this way it will be destroy there.