Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help with adding meters ran and coins collected on game over panel.

Discussion in '2D' started by TempestMiles, May 4, 2022.

  1. TempestMiles

    TempestMiles

    Joined:
    Feb 27, 2020
    Posts:
    2
    Hey there, Im new to this field ,so I'm having troubles with this part.here is my Game over panel script

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. public class UIManager : MonoBehaviour
    7. {
    8.  
    9.  
    10.     public GameObject Player;
    11.     public static bool isGameOver;
    12.     public GameObject GameOverMenu;
    13.     private void Awake()
    14.     {
    15.         isGameOver = false;
    16.    
    17.    
    18.     }
    19.     private void Update()
    20.     {
    21.         if (isGameOver)
    22.         {
    23.        
    24.             GameOverMenu.SetActive(true);
    25.             Player.SetActive(false);
    26.         }
    27.    
    28.     }
    29.  
    30.     public void RestartLevel()
    31.     {
    32.         SceneManager.LoadScene("Level");
    33.     }
    34. }
    35.  
    This is my Distance travelled script:


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using TMPro;
    6. public class DistanceTravelled : MonoBehaviour
    7. {
    8.     public GameObject startPos;
    9.     public TextMeshProUGUI distText;
    10.     public GameObject scoreTextObj;
    11.     private float distance;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         distText = scoreTextObj.GetComponent<TextMeshProUGUI>();
    16.     }
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         distance = (startPos.transform.position.x + this.transform.position.x);
    21.         distText.text = distance.ToString("F0") + "M";
    22.     }
    23. }
    24.  
    and this is my coin collector script:


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using TMPro;
    6. public class Collector : MonoBehaviour
    7. {
    8.     private float coin = 0;
    9.     public TextMeshProUGUI textcoins;
    10.     private void OnTriggerEnter2D(Collider2D collision)
    11.     {
    12.         if (collision.transform.tag == "Coin")
    13.         {
    14.             coin++;
    15.             textcoins.text = coin.ToString();
    16.              Destroy(collision.gameObject);
    17.         }
    18.    
    19.     }
    20. }
    21.  
    22.  
    So whenever i die i want the game over screen to show how much meters i ran and show how much coins i collected,based on those both values, a score should be calculated as a final score. I browsed youtube for a long time and didnt get a proper video.Thanks in advance!!
     
    Last edited: May 4, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    That's not how it works. You learn the parts, you put the parts together into your precise requirements.

    To debug what is happening, remember it might not be code: scene setup and connection is equally if not more important.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. TempestMiles

    TempestMiles

    Joined:
    Feb 27, 2020
    Posts:
    2

    I want to get the distance travelled and coin collector script into the game over panel script but i have no idea what to do.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    I'll say it again: understand the parts one part at a time:

    Get this working:

    Don't do ANYTHING else until you have that quantity in a variable that you can display with Debug.Log()

    Don't think about panels.

    Don't think about coins.

    Focus.

    One option is tutorials.

    Screen Shot 2022-05-04 at 9.11.51 AM.png

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors...

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.