Creating a Game Over after a Trigger/Event
Hello everyone! Loads of times I have found very useful answerered questions, this time, my first time writing here, I need a little more help. I am sorry in advance for not being a pro with unity. I would like some help on switching from scene to scene after an even happened in my game (I am using C# for my scripts).
So, I am making a "demo" videogame for a university project. I have made a Main Menu scene and a Game scene. This demo ends with entering a particular room with a Trigger Collider and when you hit this collider, a video starts playing.
My video player code is this one:
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);
}
}
}
then it destroyes the object and the game starts again from where I left. I would like to completely change scene (so I would make a Game Over scene) or I would like to go back to my Main Menu.
I have a code that makes me switch between Main Menu and Game:
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
But this happens when I hit the Play button on the Main Menu. Now I would like to change scene suddenly after the event happened, so it won't go through the game again.
Sorry for the long explanation and thank you even for reading this far!
Answer by tormentoarmagedoom · Jun 26, 2018 at 02:29 PM
Good day.
You are very close. As you know, all scenes have a name ,which is a string variable, and also have a int index number (the order you set them in the build window).
You can use LoadScene with the name or with the index, so if your scene with index number 2 is called "IceLevel" , this 2 lines do exactly the same:
SceneManager.LoadScene("IceLevel");
SceneManager.LoadScene(2);
Now, lets see what you do in your code. You are changing the level by its index, but instead of writing the number, you do this:
SceneManager.GetActiveScene().buildIndex
wich gives you the number of the current scene, so after adding 1 more, your final LoadScene command Load the next scene in your scene list.
If you want to control what to load then, just use the Scene name to load what you really need.
TRy it, and I'm sure you will get it!
Good luck! :D
Thank you for you kind answer!!! :D
This actually works, thank you. But I still have a problem with the code, because in this way it goes to another scene, yes, but it does not let my video play.
So, I added a simple method to the code of the video player here:
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);
GameOver;
}
}
public void GameOver()
{
Scene$$anonymous$$anager.LoadScene("GameOver");
}
}
Thinking, kinda dumb by me, that it would wait for the video. But it does not, so it does not get to play the video. I searched something and I have found some functions, like async and await, or methods like EventWaitHandle, but I don't know if it is the right path to follow. Another idea I got is to make the trigger change scene and in the Game Over scene playing a video on awake, but then it would give me the same problem: do something after the video is done!
Really thank you for the help that you gave me, I would appreciate a suggestion about this other problem, please Thank you!
Good day.
I have no idea about video playing in Unity, but you just need to detect when the video finished, and then execute the LoadScene().
I recommend you to open new question about thow to know if a video has finished, so you can get your answer.
Ah one more thig, in your code, you forget to use the "()" when calling GameOver method
GameOver();
PD: Please accpet this answer as correct, so other users can easy find the solution to your initial problem!
Bye!
Ahh, yes, of course about the brackets. Sorry! Also, thank you!! I am going to do it right now. Really thank you so much for the help! :)
Your answer
Follow this Question
Related Questions
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers
How to get the Collider other element Rigidbody? 0 Answers
Remove Door's collider if cube is in the trigger,Destroy Door's Collider if box is in the trigger 0 Answers
Destroying Object OnTriggerEnter2D not working 1 Answer
C# OnTriggerEnter Issue 3 Answers