Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by elelanfri · Nov 20, 2021 at 03:07 PM · collisionmenudeath

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
    }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Don't restart music on death 2 Answers

Scripting a Death Fall 1 Answer

I have a trouble with making game over canvas visibility on and off. 0 Answers

How to use collisions to restart a scene 1 Answer

I want my player to die when collide with enemy from front or back and not when it jumps on it (i alread tried checking if player is grounded or not) and if someone could provide script for it ill be glad 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges