Unity Tutorial How To Make Restart Button To Reload The Scene In Jetpack Clone Android Game

Unity Tutorial How To Make Restart Button To Reload The Scene In Jetpack Clone Android Game

Alexander Zotov

5 лет назад

67,419 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@AlexanderZotov
@AlexanderZotov - 10.10.2018 09:01

ALARM! ALARM! COROUTINE ISSUE DETECTED!
If you press and hold your finger at the very beginning of the game, pass through the comets without touching them, pick up the fuel can and fly off the screen without releasing your finger then fuel counter will freeze at some value and will not go down until you release and put finger back on the screen. It happens because of the coroutine. When it starts it takes the current fuel amount value (let it be 50 for example). Coroutine stops with two conditions - if finger is released or if fuel amount inside this coroutine is equal to zero. So if you pick up fuel can while coroutine is running when fuel amount equals to 50 then overall fuel amount becomes 100 back again. But inside the coroutine, it still goes down from 50. When coroutine is over (loop comes to zero) then engine should stop but it doesn't happen because fuel amount is 50 now. And it will always be 50 until you release the finger and put it back on the screen to start new coroutine which will countdown from 50 to 0.
So to fix this issue we should replace this line in BurnFuel coroutine
for (int i = fuelAmount; i >= 1; i--)
with this line
while (fuelAmount > 0)
and everything will work fine.
So coroutine code will look like this

private IEnumerator BurnFuel()
{
while (fuelAmount > 0)
{
fuelAmount -= 1;
yield return new WaitForSeconds(0.01f);

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
//if (Input.GetMouseButtonUp(0))
break;
}
}

But we should use WHILE loop inside any of the coroutine very very carefully because it can halt the running project easily.

Ответить
@t.p3325
@t.p3325 - 15.12.2022 18:47

inside on click/function/my script, is not show me the restart option

Ответить
@asasin8129
@asasin8129 - 28.02.2022 23:55

Very cool thank you!

Ответить
@PedroBastozz
@PedroBastozz - 07.01.2022 22:47

TSM!!!! IT WORKED!!! my dad just bought apple dev account for me and now i can publish my first game!!!!

Ответить
@davidtvz
@davidtvz - 06.12.2021 14:41

Brother I have spended the last day and a half triying to do this by reverse ingeniering the code I had. Thanks! this was kick and simple.

Ответить
@fedemp91
@fedemp91 - 15.09.2021 18:04

ty

Ответить
@johanalmansyah6140
@johanalmansyah6140 - 05.07.2021 10:04

please make some tutorial how to make restart button after for example one scene was locked because player has finished the game

Ответить
@thegamingprofile6797
@thegamingprofile6797 - 21.05.2021 01:58

How do you reset multiple scenes, for example if I die on the 3rd level and press go back to menu then select the the 1st level in menu, everything in game doesn't work, how do I do a complete restart of the game where each level resets

Ответить
@SwissPcGaming
@SwissPcGaming - 17.05.2021 11:20

Thank you alot! :D

Ответить
@denilseven
@denilseven - 28.03.2021 04:28

Thanks from brazil S2

Ответить
@star-devil9157
@star-devil9157 - 26.03.2021 11:22

Thank you sir it worked perfectly ❤

I'm doing for my 3d car racing game
It worked infinite % sure ❤❤❤

Ответить
@omerbaylam
@omerbaylam - 24.02.2021 23:33

THX <3

Ответить
@thatsound7057
@thatsound7057 - 14.02.2021 12:57

How can I write codes in my restart button in C# platform game ?

Ответить
@macleodgordon
@macleodgordon - 27.12.2020 19:45

Can you make a video of how to handle static variables upon a restart. It appears that when the scene reloads, the static variables still have their old values.

Ответить
@yellowdino6046
@yellowdino6046 - 14.10.2020 06:37

thank you it worked for me is amazing new sub!.

Ответить
@ZacharyAghaizu
@ZacharyAghaizu - 03.09.2020 18:06

nice and clear! thank you

Ответить
@tomobryant2070
@tomobryant2070 - 02.09.2020 01:03

This only works if your game only has one level. If you have multiple levels, the restart button will always spawn you into the level written in the script.
To make a true restart button that loads the current scene, use this code:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);

Ответить
@tanomations3300
@tanomations3300 - 25.08.2020 01:45

Please help my code is :using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RewindTime : MonoBehaviour
{
public void RestartGame()
{
SceneManager.LoadScene("Level1");

}
}
" And i get a complier error type namespace defenition or end of file as expected

Ответить
@Atskas
@Atskas - 20.08.2020 15:22

I can't find the function. Any help?

Ответить
@elver6641
@elver6641 - 07.08.2020 04:56

This is so cool

Ответить
@tomassandoval7018
@tomassandoval7018 - 26.07.2020 23:36

THANKS!! So easy

Ответить
@samudramaola7677
@samudramaola7677 - 24.07.2020 18:58

Why does my scene freeze after restarting?

Edit: nvm I forgot to put Time.timeScale = 1f;

Ответить
@MTGKMerakliTV
@MTGKMerakliTV - 19.07.2020 00:47

Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Restart : MonoBehaviour {
public void RestartGame()
{
SceneManager.LoadScene ("Scene Name);
}
}

Ответить
@mannou38
@mannou38 - 09.07.2020 16:08

You can probably set the scene that you want to reload as
SceneManager.LoadScene(SceneManager.GetActiveScene())

Ответить
@JonqsSimulations
@JonqsSimulations - 15.06.2020 18:45

thank you needed this for my game

Ответить
@_felix128old2
@_felix128old2 - 13.06.2020 13:32

y is this not working for meeeeeeeee

Ответить
@Nice7Nerdy
@Nice7Nerdy - 26.05.2020 21:44

Спасибо большое за видео! Вы мой спаситель, Александр)
Подскажите, у Вас есть видео, в котором бы объяснялось, как научить эту кнопку появляться только после смерти гг?

Ответить
@ulisesgamboa2702
@ulisesgamboa2702 - 22.05.2020 08:15

I don't know why it doesn't grab the click or the touch of the reset button. I did everything that appeared in the procedure. I even checked that the scene was the same as the code. Help!

Ответить
@STARPIG
@STARPIG - 21.05.2020 12:02

Perfect!! Fast and simple like every Unity tutorial should be.

Ответить
@DSlut
@DSlut - 19.04.2020 15:05

Thanks for the tutorial! @Alex, I've got a question. I added a restart button to my simple puzzle game with WinText (see your another guide), and it happens that WinText stays on a screen when I press restart button. How I can avoid it and make full restart?

Ответить
@mustafaaltun94
@mustafaaltun94 - 12.04.2020 17:02

You are man

Ответить
@yoursituation
@yoursituation - 08.04.2020 10:07

pls restart code write

Ответить
@glitterdustxoxo
@glitterdustxoxo - 01.04.2020 23:36

my scene freezes after pressing the restart button

Ответить
@johnnyd4827
@johnnyd4827 - 09.03.2020 12:38

simple and very clear, thank you!

Ответить
@zerprob6311
@zerprob6311 - 02.03.2020 23:02

what is the canvas?

Ответить
@youidaboss713
@youidaboss713 - 26.02.2020 11:55

So I have BG music in my game and whenever I reset the game, the music stays but another one plays again. Can you please help?

Ответить
@munirzaben1816
@munirzaben1816 - 02.02.2020 10:19

thank you but the semplescene can not Load into my projects

Ответить
@techgames6902
@techgames6902 - 20.12.2019 17:04

Great job sir

Ответить
@loweni7460
@loweni7460 - 18.12.2019 07:24

Can't find my restart button sprite in the project folder what do I do?

Ответить
@thebadwan9236
@thebadwan9236 - 26.11.2019 18:41

Thank you so much dude!!!!

Ответить
@rajanggg251
@rajanggg251 - 05.11.2019 21:58

thx man for this video

Ответить
@raomuhammadusama9244
@raomuhammadusama9244 - 26.10.2019 12:04

I am a beginner and wanted to integrate a restart button in my game. Thanks for helping me out.

Ответить
@dipanshumahla546
@dipanshumahla546 - 13.10.2019 20:00

The update function does not work after reloading the scene.

Ответить
@AF-ei5yi
@AF-ei5yi - 05.02.2019 18:26

I've been looking for a tutorial like this for a while - it's perfect. I have followed it to the letter but it doesn't work :(

Got any suggestions as to why?

Ответить
@GeddyGames
@GeddyGames - 06.12.2018 11:22

Can someone help me, i followed the code but after i restarted my game it freezes completely. Can someone help me out? thanks and muchly appreciate it!

Ответить
@sagarjain2030
@sagarjain2030 - 13.11.2018 18:55

Thanks a lot for short and simple tutorial...

Ответить
@FuzzyDPozzy
@FuzzyDPozzy - 30.10.2018 21:32

man can you please please please make a video, how to build game in unity like what sdk or what java to use and how to install them just please and thanks

Ответить
@mikotv9836
@mikotv9836 - 10.10.2018 12:34

Amazing,please keep it up uploading your awesome tutorials?

Ответить
@a.mused22
@a.mused22 - 10.10.2018 03:44

i love de simplicity of your videos. Unity for family

Ответить