Problems with "Sam's Teach Yourself" 2018 Edition; PlayerRespawn failing?

So I’ve decided to try and jump back into learning Unity and I picked this book back up, “Sam’s Teach Yourself Unity 2018 Game Development” by Mike Geig. I rolled back from 2019 to 2018.4.22f after experiencing issues, and got all the way to Hour 6 before I got stuck. I’m at the point where I’ve made the landscape and everything for “Amazing Racer”, but when I try to implement the “PlayerRespawn.cs” script onto the WaterHazardDetector, nothing seems to happen. The character crosses the threshold, the screen flicks back to the start for maybe two or three frames, and then I’m back in the water. Furthermore, when I reach the end point, It finishes and gives me the time, but if I try to restart by clicking restart, it just restarts me at the position I finished at, which immediately ends it and I can’t restart it proper without relaunching the game. I get no errors when compiling, though, and I have all the spots filled in, so I don’t understand what the problem is.

The code for PlayerRespawn.cs is:

using UnityEngine;
using System.Collections;

public class PlayerRespawn : MonoBehaviour
{
    //A reference to the game manager
    public GameManager gameManager;

    // Triggers when the player enters the water
    void OnTriggerEnter(Collider other)
    {
        // Moves the player to the spawn point
        gameManager.PositionPlayer();
    }
}

And the code for the Game Manager is this:

using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class GameManager : MonoBehaviour
{   
    // Place holders to allow connecting to other objects
    public Transform spawnPoint;
    public GameObject player;

    // Flags that control the state of the game
    private float elapsedTime = 0;
    private bool isRunning = false;
    private bool isFinished = false;

    // So that we can access the player's controller from this script
    private FirstPersonController fpsController;


    // Use this for initialization
    void Start ()
    {
        // Finds the First Person Controller script on the Player
        fpsController = player.GetComponent<FirstPersonController> ();
   
        // Disables controls at the start.
        fpsController.enabled = false;
    }


    //This resets to game back to the way it started
    private void StartGame()
    {
        elapsedTime = 0;
        isRunning = true;
        isFinished = false;

        // Move the player to the spawn point, and allow it to move.
        PositionPlayer();
        fpsController.enabled = true;
    }


    // Update is called once per frame
    void Update ()
    {
        // Add time to the clock if the game is running
        if (isRunning)
        {
            elapsedTime = elapsedTime + Time.deltaTime;
        }
    }


    //Runs when the player needs to be positioned back at the spawn point
    public void PositionPlayer()
    {
        player.transform.position = spawnPoint.position;
        player.transform.rotation = spawnPoint.rotation;
    }


    // Runs when the player enters the finish zone
    public void FinishedGame()
    {
        isRunning = false;
        isFinished = true;
        fpsController.enabled = false;
    }
   
   
    //This section creates the Graphical User Interface (GUI)
    void OnGUI() {
       
        if(!isRunning)
        {
            string message;

            if(isFinished)
            {
                message = "Click or Press Enter to Play Again";
            }
            else
            {
                message = "Click or Press Enter to Play";
            }

            //Define a new rectangle for the UI on the screen
            Rect startButton = new Rect(Screen.width/2 - 120, Screen.height/2, 240, 30);

            if (GUI.Button(startButton, message) || Input.GetKeyDown(KeyCode.Return))
            {
                //start the game if the user clicks to play
                StartGame ();
            }
        }
       
        // If the player finished the game, show the final time
        if(isFinished)
        {
            GUI.Box(new Rect(Screen.width / 2 - 65, 185, 130, 40), "Your Time Was");
            GUI.Label(new Rect(Screen.width / 2 - 10, 200, 20, 30), ((int)elapsedTime).ToString());
        }
        else if(isRunning)
        {
            // If the game is running, show the current time
            GUI.Box(new Rect(Screen.width / 2 - 65, Screen.height - 115, 130, 40), "Your Time Is");
            GUI.Label(new Rect(Screen.width / 2 - 10, Screen.height - 100, 20, 30), ((int)elapsedTime).ToString());
        }
    }
}

I’ve also included screenshots where the spawnpoints and finish points are defined with relation to starting, water hazard detection, respawning, and finishing.


I’ve noticed that when I start the game, doesn’t start me at the starting point like I defined, but rather from where my player positioned, and I’m wondering if there’s something I’ve missed, because collisions and resets are kind of important with regards to game development and not exactly something I can just put off, and I’ve spent much longer than just an hour trying to figure out what I’m doing wrong.

As a side note that I forgot to bring up, I had to import the Standard assets after installing Unity, so I am unsure if maybe that might have to do with the error or not.

Are you sure that the reference to the player GameObject is to the player in the scene and not the prefab in your project?
If that is ok you can try Debug log the position of the spawn point to make sure it where you expect it to be.

I’m pretty sure. I took the prefab and put it into the scene, and then put the reference from the scene to the GameManager.

I don’t know how to access the debug log outside of looking at the console built in, and that isn’t showing anything. My knowledge of this is very limited and unfortunately surface level.

i’m in the same boat although i managed to adapt it to 2019.x.x… I’m brand new to unity but have a good amount of development experience but mostly in web applications. That being said, if @Thanatoast250 's issue appears to be the same as mine, the issue seems to be in the repositioning part of the respawn code.

I’ve thrown some logging into the PositionPlayer method and it definitely hits so i know the collision detection is working. My position player method looks like this (let’s ignore everything besides the x coord including rotation, although it’s also not working):

    public void PositionPlayer()
    {
        Debug.Log("Current player positionx " + player.transform.position.x);
        Debug.Log("collided!, positioning to the player " + spawnPoint.position.x);
        player.transform.position = spawnPoint.position;
        Debug.Log("new positionx " + player.transform.position.x);
        player.transform.rotation = spawnPoint.rotation;
    }

Because PositionPlayer gets called when StartGame gets called, you’ll see debug logging appear when the game starts and if you have to respawn. Here is my output:

i put a Log in start game as well. the span point should be and is (165, 32, 125). But i’ve manually repositioned my Player to 170 in the UI and when i play the game you can see the logging says it’s about 170 (169.11), then PositionPlayer gets called and sets it to 165. BUT in the game it immediately snaps back to 170 almost like there is some sort of commit action that needs to happen when adjusting a transform property?

at 12:59:41 you can see i jump into a hole that should “kill” me and initiate the respawn. The collision is detected, we enter the function, it sets the position, and the player.transform.position.x is recorded to be 165 but no actual repositioning is evident in the game, the player is just stuck in the hole (or at the end game position). i’ve also tried setting the position to be a new Vector3 with no luck.

Any help would also be appreciated :slight_smile: Sorry to potentially clutter the thread but i think our issues are identical.

thanks
bret

Yes, there was a very specific issue with that version of Unity and being able to move the rigidbody via script. It has been a while so I don’t remember the exact checkbox in the physics settings you need to check to make this work. I am away from my work machine right now but as soon as I am back at a machine with Unity on it, I will look and report back. Mostly I wanted to respond now and tell you that it’s definitely not something you are doing wrong! There is just a setting that is usually on, but was off in this version.

Sorry for the delay. If you go to Edit > Project Settings > Physics, look at your settings. This is how they should be:

2 Likes

That fixed it for me! Thanks so much!

Thank you very much! That fixed it for me! Version 2020.3.33f1 Personal

Thank you dear friend!

Thank you ! Ver. 2022.3. Never would have guessed it.
Now can anyone explain why this parameters should be the way they should and why are they not enabled by default?
I’d like to get to the root of the problem)

Hello, guys. I am very new to Unity and not sure what I’m doing wrong. I redid the project three times and it is still not working. When I press run, it would not automatically go to game mode, which is fine because I can just click it. But the thing is, when I try to click start or press enter, nothings happens. It gives me an Error saying, Unable to add Renderer to the Scene after Culling. Possible cause: A camera callback, such as OnPreRender, called Graphics.DrawMesh… I do not have two main cameras. The only cameras I have is the ones for Water4Advanced. Please help!

Hmmm, I am not sure why you would be seeing that error at all. Could you answer:

  • What Unity version?
  • Which project you’re creating?
  • Have you tried closing Unity and reopening it?

Also, can you provide a screenshot of your editor setup?