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

Deactivate Object Between 15-45 Seconds Of Not Visible

Discussion in 'Scripting' started by Divis10nStudios, Apr 13, 2019.

  1. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    195
    Like the title says, is there a way to deactivate an object randomly between a given time? I know there's something like OnBecameVisible but is there an OnBecameInvisible or something like that?
    **EDIT**
    After some messing around i managed to do something but is there a way to fade the audio out from the audio source I attached (not referenced in script).
    Code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChaseDeactivate : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float delayBeforeLoad;
    9.     [SerializeField]
    10.     public GameObject killerObj;
    11.     private float timeElapsed;
    12.  
    13. void Start()
    14. {
    15. delayBeforeLoad = Random.Range(15.0f, 45.0f);
    16. }
    17. private void Update()
    18. {
    19. timeElapsed += Time.deltaTime;
    20.  
    21.  
    22. if (timeElapsed > delayBeforeLoad)
    23. {
    24. Destroy(killerObj);
    25. }
    26. }
    27. }
    28.  
     
    Last edited: Apr 13, 2019
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Starting a Coroutine that fades audio, then disables the gameObject when done. If I remember correctly it's faster to change the gameobjects visibility and disable scripts than it is to disable the gameObject entirely ( Haven't been using Unity as much recently )
     
  3. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    195
    oh ok thanks