Game

Unity Endless Runner Tutorial #11 – Death & Restarting




In this episode, we’re going to create a system to restart the game when the player is killed by falling off a platform.

Get the scripts used in this series by supporting on Patreon at:

Get the art asset’s used in this episode from Kenney here:

Don’t forget to hit that like button and if you’d like to see more gaming goodness then subscribe for more!

And you can also find me on twitter and facebook!
Twitter –
Facebook –

Outro music “Theme for Harold var 1” by Kevin McLeod –

Nguồn: https://butthun.com/

Xem thêm bài viết khác: https://butthun.com/game/

Game
ASMR classic cranial nerve exam
Game
INSANE LUCKY!! opening Mega Box in Brawl Stars
Game
House Paint – Android/iOS Gameplay ᴴᴰ
  • Those who are getting the catcher error,Make sure you make the coRoutine to IEnumerator, hope it helps


  • I am type the code properly and checked it multiple times… When I start the game it works fine, but when I die and restart, after a few seconds my player character starts running a million miles an hour all of a sudden. please help me


  • for some reason, after i implemented the code here, my player character does not want to register that it is grounded after it touches a new platform


  • Can I still get help here ? Just stuck on an error i keep getting. Player and platforms starts out fine, once player dies, the restart of the platforms are all the at the beginning of the scene and not where it should start originally. Any ideas? Also keep seeing this warning 'GameManager.PlayerStartPoint' is never assigned to, ? Any idea on that anyone?

    Thanks


  • My dumbass couldn't get the restart thing working…

    I spelt Collision wrong "Collison"


  • aaaaaaaam when the game gets restarted the platform gen works but the first 3 or 4 platforms are geting generated invisible and then it goes on with normal generation

    what do i do


  • hi.
    I'm using Unity 2018.4.12f1 (64-bit).
    public PlayerController in the gameManager script does not allow me to create thePlayer.

    error "CS0246: The type or namespace name 'PlayerController' could not be found (are you missing a using directive or an assembly reference?)"

    How can I fix?


  • for anyone having a problem with on collision enter try OnCollisionEnter2D


  • if(Other.gameObject.tag == "Killbox")

    {

    GameObject.Find("GameManager").GetComponent<GameManager>().RestartGame();

    }

    works for me, for now at least, im in a later version of Unity, also this creates another problem of the platform spawning over where you respawn making it almost impossible to get out of spawn.
    if anyone has a suggestion that would be great.


  • my unity can't find the object <PlatformDestroyer>


  • the clones for the 7×1 platforms does not have the platform destoryer script, is that normal? because my platforms are still overlapping each other after the game restarts. how do i solve this problem?


  • Hey. I have a problem. After death my game stayed is paused. Why. Please help!


  • I was getting null object something instance… when I hit the killbox and was not dying. I missed the part where he dragged the game manager onto the player. 17:40.


  • my game is not restarting and getting the following error as soon as the player hit the Catcher
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/PlayerController.cs:92


  • Why not use load scene to restart?…


  • Followed the code, labeled the 'catcher' as killbox, but my character can still use the element to jump back, not die. Why won't it trigger death?


  • James, you are the best thanks for these free classes,
    but I have a doubt:

    Why do you create a milestone?
    Can't we do it this way?
    float fl = transform.position.x
    speed = 6 + fl * 0.02f;
    if (maxSpeed < speed)
    { maxSpeed = speed;
    }

    // it'll increase the speed slowly according to our distance traveled.


  • I don't know if it's useful but if you have the problem that the player don't die in the collision with the killbox : Try to change the "OnCollisionEnter2D" by "void OnTriggerEnter2D(Collider2D other) " for me it work good now.


  • after the game restarts the platformDestructionPoint is set to false, how do I fix this?


  • Hey guys, if player won't die. Double check IEnumerator. Was stuck for a while with IEnumerable instead haha


  • 23:15 You can also use a foreach loop:
    foreach (PlatformDestroyer destroyer in platformList) {
    destroyer.gameObject.SetActive(false);
    }
    If you're wondering why you'd use a foreach loop instead of a for loop:
    Benefits:
    +Readability. Readability is always important since it'll save you or others who need to use the code in the future a LOT of time. This is pretty much the only reason, but it is a very important reason.

    Drawbacks:
    -In C# a foreach loop is ever so slightly slower than a for loop. Usually computers are fast enough, especially if you're on a level where I need to explain this. Unless you're often looping over huge arrays you won't notice any difference. (In C++ a "for(auto x in list)" is quite a bit faster than "for(int i=0; i<list.size(); i++)". So do your research if you're going to use other languages)
    -In a for loop you can access the current value "list[i]", but you can also access the next value "list[i+1]" or the previous value "list[i-1]".

    (disclaimer: I study software engineering for just over 1.5 years now, so there could be more benefits and/or drawbacks I'm not aware of.)


  • Having problems with the line
    yield return new WaitForSeconds(0.5f);
    it doesn't work for me so I have to have
    yield return new WaitForSeconds(Seconds / 2);
    with
    private const float Seconds = 1.0f;
    just above
    public Transform platformGenerator;
    any solutions?


  • Hey there! How could you do this without any player? (The thing is that I am creating a falling words typing game and I think there is no player and what I wanted to do is that when the words fall to a certain point it is game over)


  • my platforms don't seem to enter my array in the game manager to deactivate all the platforms after death, i know this video is pretty old but if anyone could help i'd appreciate it thanks.

    This is how far/what i have so far:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameManager : MonoBehaviour {

    public Transform platformGenerator;
    private Vector3 platformStartPoint;

    public PlayerController thePlayer;
    private Vector3 playerStartPoint;

    private PlatformDestroyer[] platformList;

    // Use this for initialization
    void Start()
    {
    platformStartPoint = platformGenerator.position;
    playerStartPoint = thePlayer.transform.position;

    }

    // Update is called once per frame
    void Update()
    {

    }

    //lets things activate this code outside this script
    public void RestartGame()
    {
    StartCoroutine("RestartGameCo");
    }

    public IEnumerator RestartGameCo()
    {
    //turns off the player object
    thePlayer.gameObject.SetActive(false);

    //waits half a second before running code
    yield return new WaitForSeconds(0.5f);

    //finds all the platforms with the platform deactivator/destroyer
    platformList = FindObjectsOfType<PlatformDestroyer>();
    for(int i = 0; i < platformList.Length; i++)
    {
    //deactivates the active objects after death
    platformList[i].gameObject.SetActive(false);
    }

    //moves player to beginging
    thePlayer.transform.position = playerStartPoint;

    //moves platform generator to begining
    platformGenerator.position = platformStartPoint;

    //reactivates the player
    thePlayer.gameObject.SetActive(true);
    }

    }


  • my speed increase milestone doesn't reset, instead it goes to 0 please help


  • My player is not resetting its position rather it starts from different position
    Can somebody help i have written the same code 3 times??


  • So I did the same code as you and 2 things happened first the incresingmiles do not fit alone I have to adjusting to start putting 50. The second mistake is that when he dies the persoangem stops moving, he just jumps in the same place. The code stores all the information you need to save.


  • It's been so long but… My platformDestructionPoints are being destroyed when the plataforms go active(false). Also, my GameManager affects my ObjetsPooling and they won't work anymore. Does anyone know how to fix this?


  • Hey, I don't know if anyone is going to answer me, but anyways, I did exactly the same as the tutorial but when my character died, it didn't restart, it seemed that the void OnCollisionEnter2D() wasn't called, also, I was scrolling in the coments to see if anyone had the same thing, and I tried doing all your solutions but they didn't work. Any other solution? ASAP pls


  • 200 likes!!! CONGRATS.


  • Can't find this anywhere else, but when I put GameManager in the playercontroller script it doesn't show up as a variable in inspector, Any ideas?


  • i am experiencing a null pointer in this line if (transform.position.x < platformDestructionPoint.transform.position.x) in platform Destructor script saying that Object Reference Not Set to Saved Instance of an Object . can anyone help me with this i am stuck at this : )


  • Im having a issue when dying, "Object reference not set to an instance of an object." then the problem with the code is a problem with platformdestroyer

    void Update() {
    if (transform.position.x < platformDestructionPoint.transform.position.x)


  • RoyRoy

    Author Reply

    the Line — FindObjectOfType<PlatformDestroyer>(); — shows error CS0029: Cannot implicitly convert type 'PlatformDestroyer' to 'PlatformDestroyer[]' ….. pls help


  • Only now I full undertanded corrotine! Thanks, Master!!!


  • So I didn't add the catcher platform in the previous tutorial I did for this one, but my game won't restart whent he player dies.


  • followed the script exactly but the player doesn't die/restart on hitting the catcher:/ very new to c# and unity but so far every things gone perfectly until now