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;
}
}
}