Search Unity

Disabling a gameobject after a certain amount of time after enabling it?

Discussion in 'Scripting' started by MSachs, Jan 29, 2018.

  1. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Hi,

    I am playing a video when pressing a button on screen through enabling an empty gameobject which has the videoplayer component on it. The videoplayer is disabled in a start function.
    After the videoclip has finished I would like the gameobject to be disabled again automatically.

    Is there a way to check if the videoclip has ended?

    If not: how would I start a timer at the time of enabling the gameobject so the time can get set manually after which the gameobject should be disabled again?

    Until now I only found similar threads which seem to work after a certain amount of time since the start of the application which would not work since the user can press the button at any time...

    Thanks in advance.
     
  2. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    you could use a simple IENumerator or check for VideoPlayer.isPlaying
     
    MSachs likes this.
  3. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Thanks I got it to work with an IEnumerator and WaitForSeconds :)
     
  4. duisti

    duisti

    Joined:
    Nov 29, 2017
    Posts:
    52
    Can also make a function which only purpose is disabling the gameobject and using Invoke("yourfunction", YourTimeHere);
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    No need for a coroutine / timer approach, you can use the event loopPointReached.
     
    MSachs likes this.
  6. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    That seems definitely better than a timer. But how would I use this? Can't find any scripting reference in Unity's documentation. :)
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    MSachs likes this.
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If is works like documented, it should be better and cleaner, indeed. You won't need extra code except the subscriber for the event. You also don't face the problem that the timer doesn't know when a video is paused etc, unless you pause it as well etc.
    I haven't used this yet, but you might still wanna play around and see whether there are cases you still need to handle.
     
    MSachs likes this.
  9. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    Suddoha likes this.
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) Cool.
     
    MSachs likes this.