Respawn Player GameObject after falling a certain Y distance

Hello all,

I wanted to force the Player gameObject to respawn if it falls to a certain Y value (-20). However, for some reason this throws an error:
The name `Playercube’ does not exist in the current context.

Well, I’m sure I had to store the transform variables somewhere, so I created a public GameObject PlayerCube to store the Player GameObject. This error has me confused. Any assistance will be appreciated. Thank you!

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

public class PlayerMovement : MonoBehaviour {
    public static PlayerMovement jumpForceModify;
    public float speed = 10.0f;
    public float jumpForce = 20.0f;
    public float gravity = 30.0f;
    private Vector3 moveDirection = Vector3.zero;
    public GameObject PlayerCube;

    void Start()
    {
        jumpForceModify = this;
    }
        void Update()
        {
            CharacterController controller = gameObject.GetComponent<CharacterController> ();

        if (controller.isGrounded)
        {
            moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
            moveDirection = transform.TransformDirection (moveDirection);
            moveDirection *= speed;
        }

        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move (moveDirection * Time.deltaTime);

        if (PlayerCube.transform.position.y < -20)
        {
            PlayerCube.transform.position.x = 0;
            PlayerCube.transform.position.y = 0.5;
            PlayerCube.transform.position.z = 0;
        }
        }
}

what line are you getting the error on?
if it’s line 34, then add an f
PlayerCube.transform.position.y = 0.5f;

The lines are 31, 33, 34, and 35 re: The name `Playercube’ does not exist in the current context.

if the script PlayerMovement is attached to the player then you don’t need PlayerCube
you can change the code to this

if transform.position.y < -20)
        {
           transform.position.x = 0;
            transform.position.y = 0.5;
            transform.position.z = 0;
        }

if the script is not attached to the player then verify you have the value set in unity. you may have to comment out the lines that are having issues.

I don’t see any reason why it’s complaining. What are the chances you have PlayerCude as a static variable in another script? you can test this by simply changing the PlayerCube to something like _PlayerCube or MyPlayerCube