- Home /
Can't display the menu when player dies
Hi! I want dispay my menu when the player dies (on collision) but I can't. Here my script attached to player:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMove : MonoBehaviour { private CharacterController controller; private Vector3 direction; public float forwardSpeed;
private int desiredLane = 1; // 0: left 1: middle 2:right
public float laneDistance = 4; // distance between two line
public float jumpForce;
public float Gravity = -20;
public bool isDead=false;
void Start ()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (isDead)
return;
controller.Move(direction*Time.deltaTime);
direction.z = forwardSpeed;
if (controller.isGrounded)
{
direction.y = -1;
if(Input.GetKeyDown(KeyCode.UpArrow))
{
Jump();
}
} else
{
direction.y += Gravity * Time.deltaTime;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
desiredLane++;
if (desiredLane==3)
desiredLane=2;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
desiredLane--;
if (desiredLane==-1)
desiredLane=0;
}
Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
if(desiredLane == 0)
{
targetPosition+= Vector3.left * laneDistance;
} else if (desiredLane==2)
{
targetPosition += Vector3.right * laneDistance;
}
transform.position = Vector3.Lerp(transform.position, targetPosition, 80*Time.deltaTime);
// controller.center = controller.center; //per far funzionare il collider correttamente
}
private void Jump()
{
direction.y = jumpForce;
}
public void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.point.z > transform.position.z + controller.radius)
Destroy(gameObject);
// Death();
}
public void Death()
{
isDead = true;
}
}
Here my script attached to menu:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DeathMenu : MonoBehaviour { PlayerMove playerDead;
void Start()
{
gameObject.SetActive (false);
playerDead = gameObject.GetComponent<PlayerMove>();
}
void Update()
{
if (playerDead.isDead)
{
gameObject.SetActive (true);
}
}
}
Thanks for answering!
Answer by logicandchaos · Nov 20, 2021 at 03:35 PM
Line 73 // Death(); is commented out, so isDead never gets set to true, which doesn't matter because the line above that you destroy the player object that has the PlayerMove script that has the variable isDead. If you destroy the object you can't read the value.
You should be coding this reactively. You shouldn't be using update in DeathMenu. You can have a method ShowMenu(){gameObject.SetActive (true);}
Then when you die you call that method.
Answer by gipsnichmeer · Nov 20, 2021 at 03:41 PM
You did a couple mistakes.
Firstly, you commented out Death();
Secondly, if you want to do multiple things in an if statement, you need to use brackets.
Thirdly, when you destroy the gameobject first, the script gets destroyed too and can't do what its supposed to do after it the Destroy(gameObject);
So just don't do anything after destroying the object
I fixed all 3 mistakes for you:
public void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.point.z > transform.position.z + controller.radius){
Death();
Destroy(gameObject);
}
}
Your answer
