Detecting when a video finished playing
Hello there! I need some help, referring to a question I have already asked ( https://answers.unity.com/questions/1522261/creating-a-game-over-after-a-triggerevent.html ). So, I am trying to change my scene after a video is playing, but the problem now is that I am not sure how to make my game understand that the video has finished.
So, I have a trigger collider. When I enter this trigger, my video starts playing. Attached, here, the script:
public GameObject videoPlayer;
public int timeToStop;
// Use this for initialization
void Start()
{
videoPlayer.SetActive(false);
}
// Update is called once per frame
void OnTriggerEnter(Collider player)
{
if (player.gameObject.tag == "Player")
{
videoPlayer.SetActive(true);
Destroy(videoPlayer, timeToStop);
}
}
}
This is the code I have attached to the trigger. That "time to stop" is a "delay time", let's call it like that, that I can set--- that does what the name tells, sets a time to stop. I would like to add a Game Over method WHEN the video stops playing, but I would like some suggestions on how to do that. I have seen some different methods, but I am not sure.
Also, I was thinking about detecting when the object gets destroyed, since there is a Destroy method? But, still, I would really appreciate some suggestions for that. Thank you so much!!
Answer by tormentoarmagedoom · Jun 27, 2018 at 02:56 PM
Good day again!
I see you didnt made any research by your own.... And you should... you will be obstructed every time if dont learn how to "solve new unexpected problems" like this.
As i said yesterday, i never used videos in unity, but with only 25 seconds, i find the solution... so you could at least tried...
https://docs.unity3d.com/ScriptReference/Video.VideoPlayer-isPlaying.html
As it says, yo umust wait a little once you do videoplayer.play(), before check if is still playing, because diring first moments is still preparing the video.
If you are lazy to read the API of Unity, you are lazy for developing a game..
Bye!
Hello there! I am sorry, I think you misunderstood. Let me explain: I have done my researches by my own, I have spent time searching for informations. Yes, I have read the API of Unity. I have done everything before asking my previous question. After all my researches and after finding out that I don't know how to solve my problem, I have asked for help in the forum here. I don't know why I can't solve my problem? $$anonymous$$aybe there's something wrong in my code or something wrong with the approach I have used. But, believe me, I have spent time on that code. Also, to be honest, I am new to Unity and I am new to C#, so, since I have posted the code twice, I was thinking "ehi, maybe someone with better skills than me can see that there is an error where I don't see it".
Again, I have tried and tried with this code and as my last option I have decided that was better to ask. I am sorry you misunderstood, probably I did not explain myself in the previous post. Or, maybe, I misunderstood your suggestion to ask a new question. I am sorry for that. Also, please, don't assume people are lazy just from barely two questions? Thanks!
Bye!
Answer by lilyakkuma · Jun 29, 2018 at 11:07 AM
Hello everybody again! Since I have seen there have been LOADS of people following this question, I wanted to post another answer since I have found the correct method. So, the main reason I never got the correct code, is because I was thinking "ehi, I am destroying the object, so maybe I can rely on that". So, since I had
public GameObject videoPlayer;
I was keeping and keeping putting a videoPlayer.activeInHierarchy == false or something. The solution is easier than that!! Don't rely on the fact you have your object destroyed, but consider a Coroutine.
My final code is:
public class VideoPlayerTimer : MonoBehaviour
{
public GameObject videoPlayer;
public int timeToStop;
public bool activeInHierarchy;
// Use this for initialization
void Start()
{
videoPlayer.SetActive(false);
}
// Update is called once per frame
void OnTriggerEnter(Collider player)
{
if (player.gameObject.tag == "Player")
{
StartCoroutine(CoFunc());
videoPlayer.SetActive(true);
Destroy(videoPlayer, timeToStop);
}
}
IEnumerator CoFunc()
{
yield return new WaitForSeconds(14);
SceneManager.LoadScene(0);
}
}
Basically, I did the "most obvious thing": you put a StartCoroutine function when your player hits the collider and the video starts. That method will call the IENumerator and literally will wait for some time before loading the new scene. Now, my video lasted 14 seconds and 14 seconds is actually the perfect amount of seconds I needed- maybe you have to change it, maybe you want to change the time to stop, still it works. After these seconds, it will bring you to the new scene (in my case in my Main Menu, level 0).
That's it!! Sorry for all the troubles and sorry for putting another comment, I was just really confused and I had to change my point of view. Hope it helps somehow someone!
Answer by khalil-H · Dec 23, 2019 at 03:14 PM
you can see it: https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
Answer by unity_e-SXBZw0j2m2LA · Jul 21, 2020 at 12:50 PM
This is old but if you stumbled upon this thread like I did this is what you're looking for