My Death() method is working on collusion but fails to get deathMenu when i fall

Good evening stackoverflow folks. It is my first post here and I started studying Unity a month ago, therefore if this question seems stupid to you i apologize for it beforehand.

I have basically 3 scripts working and i followed N3K's Endless Runner tutorial on YouTube. The first script is PlayerMotor and calls the OnDeath method of PlayerScore script when our controller hits an obstacle. OnDeath method of PlayerScore script calls the ToggleEndMenu method of DeathMenu script which sets active the DeathMenu gameobject with the score we have on death.

PlayerMotor script:

 void Update()
 {
       if (transform.position.y < -1)
         {
             Death();
         }
 }
 private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
         {
             Death();
         }
     }

     private void Death()
     {
         isDead = true;
         GetComponent<PlayerScore>().OnDeath();
     }

PlayerScore script:

public void OnDeath()
     {
         isDead = true;

         if (PlayerPrefs.GetFloat("Highscore") < score)
         PlayerPrefs.SetFloat("Highscore", score);

         deathMenu.ToggleEndMenu(score);
     }

DeathMenu script:

public void ToggleEndMenu(float score)
     {
         gameObject.SetActive(true);
         scoreText.text = ((int)score).ToString();
         isShown = true;
     }

So my problem is when i hit an obstacle Death() method works perfectly fine and my death menu pops up with my score immediately. However when my character falls from a bridge or something my Death() method in Update class stops the score counter on screen but doesnt set active DeathMenu gameobject so my death menu doesnt pop on my screen. I'm really confused why it doesnt work. Can someone help me with this? Note: I deleted unrelated lines of codes from my scripts.

In general, please avoid details related to your game in questions, so they are more searchable.

Your problem is somewhat generic, your method gets called in one situation, but is not called in another.

While not really an answer, please examine an approach that should help you track your problem. Just Add lots of Debug.Log() calls in each possible place, this way you can track how your call travels. While your code looks relatively simple, I battle with similar bugs with my own code often.

What I do is just spam Debug.Logs() on every entry and exit from a branch. when the program runs, I can compare what I think happens, to what actually happens.

in your code I can see several things that could cause the execution to stop. scoreText could be null, playerprefs could not have a Highscroe key, playesrscore component could be missing, or OnControllerColliderHit method isn't called.

public void OnDeath()
     {
         Debug.Log("on death started");//debug
         isDead = true;
         if (PlayerPrefs.GetFloat("Highscore") < score)
         PlayerPrefs.SetFloat("Highscore", score);
         deathMenu.ToggleEndMenu(score);
         Debug.Log("on death finished");//debug
     }
public void ToggleEndMenu(float score)
     {
         Debug.Log("toggle end menu called with score"+score);//debug
         gameObject.SetActive(true);
         scoreText.text = ((int)score).ToString();
         isShown = true;
         Debug.Log("toggle end menu finished");//debug
     }
 void Update()
 {
       if (transform.position.y < -1)
         {              
         Debug.Log("calling death from update started");//debug
         Death();
         Debug.Log("calling death from update finished"); //debug
     }
 }


private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         Debug.Log("on colliderhit entered");//debug
         if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
         {
             Death();
         }
Debug.Log("on colliderhit finished");//debug
     }

     private void Death()
     {
        Debug.Log("on death entered");//debug
         isDead = true;
         GetComponent<PlayerScore>().OnDeath();
           Debug.Log("on death finished");//debug
     }

When you click on a debug line in the console, Unity displays a stack trace dump, showing you exact execution path (including line numbers) that led to the moment in time when the debug line is printed.

While spaming Debug messages is not a great practice, it is sometimes more convenient than using a full debugger.

Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

What if you try to set active you deathMenu right from OnDeath().

On Death and Dying (Reprint Edition). 1997 by Elisabeth Kubler-Ross, Scribner, 288 pages. How To Go On Living When Someone You Love Dies. 1991. by Therese A. Rando, Bantam, 352 pages. Final Gifts: Understanding the Special Awareness, Needs, and Communications of the Dying . 1997. by Maggie Callanan & Patricia Kelley, Bantam, 256 pages.

Instead of checking if the player is beyond a certain y point, I added a box collider under my all re-instantiated prefabs and added this line of code to my PlayerMotor and it works like a charm. Nevertheless, i will definitely use @zambari 's approach to why it didnt work in the first place. Thanks for the help.

 private void OnControllerColliderHit(ControllerColliderHit hit)
 {
     if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
     {
         Death();
     }
     if (hit.point.y < transform.position.y + 0.5f && hit.gameObject.tag == "Enemy")
     {
         Death();
     }
 }

Death research in the early 1980s blazed the trail of "willful dying." Three decades ago, great strides were made in the area of “death research,” a field that continues to grow as interest in

If you would indeed behold the spirit of death, open your heart wide unto the body of life, for life and death are one, even as the river and the sea are one. In the depth of your hopes and desires lies your silent knowledge of the beyond. And like seeds dreaming beneath the snow, your heart dreams of spring.

When being "In Treatment" doesn't work out so well. Open mobile menu Why Psychotherapists Fail To Help People Today but especially since 9-11 and the near-depression that began in the fall

But death is a process. It’s not a black-or-white line [4] . A more recent study found that rats show an unexpected pattern of brain activity immediately after cardiac arrest.

Comments
  • Why is "Can someone help me?" not an actual question?
  • Is transform.position.y < -1 ever true?
  • Thanks for the approach, although i fixed the problem by using another way I will use this to figure out whats wrong in my projects.