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

Resolved score counter not working :(

Discussion in 'Scripting' started by cowsharts, Dec 18, 2022.

  1. cowsharts

    cowsharts

    Joined:
    Dec 16, 2022
    Posts:
    8
    i have a score counter for that whenever the enemy is hit the score should go up by one
    im using this guide: https://levelup.gitconnected.com/building-a-score-system-with-ui-elements-in-unity-ef11df8ad9e7 (even though the guide is for 2d I've used it before in 3d and its worked perfectly, idk why its not working now)

    no idea why its not working
    maybe be related to uIManager, but in previous projects where I've used this code it still works

    any help appreciated
    thanks :)

    script attached to enemy:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class EnemyScore : MonoBehaviour
    7. {
    8.     private ScoreManager uIManager;
    9.  
    10.     private void OnCollisionEnter(Collision collision)
    11.     {
    12.         // checking if the UI manager is null so that the score can be updated
    13.         if (uIManager != null)
    14.         {
    15.             // shows that the score iss being updated by one point only
    16.             uIManager.UpdateScore(1);
    17.         }
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         // acessing the game score in the UI
    23.         uIManager = GameObject.Find("Canvas").GetComponent<ScoreManager>();
    24.     }
    25. }
    26.  
    script attached to canvas:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.UI;
    6.  
    7. public class ScoreManager : MonoBehaviour
    8. {
    9.     [SerializeField] private int score;
    10.     [SerializeField] TMP_Text scoreText;
    11.  
    12.     void Start()
    13.     {
    14.         score = 0;
    15.     }
    16.  
    17.     public void UpdateScore(int points)
    18.     {
    19.         score += points;
    20.  
    21.         scoreText.text = "SCORE: " + score.ToString();
    22.     }
    23.  
    24. }
    25.  
     

    Attached Files:

    Last edited: Dec 18, 2022
  2. Obscure045

    Obscure045

    Joined:
    Aug 2, 2022
    Posts:
    14
    Your code seems to be working fine. Have you tried restarting Unity?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Except of course that it's not. Is any of it even running? Find out!

    Photographs of code are not a thing. 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/

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    Beyond that, here is how to get started debugging:

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

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    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.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    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/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    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

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.

    If you're just getting a nullref, then go fix it.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  4. cowsharts

    cowsharts

    Joined:
    Dec 16, 2022
    Posts:
    8

    wow thanks !! ill definitely switch to code tags right away
    and ill keep testing all my code :)
     
  5. cowsharts

    cowsharts

    Joined:
    Dec 16, 2022
    Posts:
    8
    taking in what you've said and using "Debug.Log" it seems that in the enemy score script where i have oncollsionenter that there is no collision between the enemy and the raycast
    however in a seperate script where i have the enemy's health i know that the collision works bc of a debug message and that the enemy is destroyed after 10 hits

    would there be interference with the script as i know my raycast works ?
    would there be an issue if i have three scripts attached to the enemy ?

    thanks again :)
     
  6. cowsharts

    cowsharts

    Joined:
    Dec 16, 2022
    Posts:
    8
    UPDATE:

    it now works !!!
    turns out the solution was to have the entire enemy score script into my enemy health script
    i believe the main issue was the 'oncollisionenter' as my enemy health script has a different event function

    but thanks again for all your help
    i will keep your advice in mind when moving forward and i will continue to improve my skills

    thank you :)
     
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    You're welcome!

    It's all about isolating exactly what is failing: see how you isolated that the collision wasn't happening? That meant you no longer wasted time anywhere else until it worked... and there you go, that was the only problem.

    Sometimes there are series of problems and you have to solve them one at a time, which is why it's always best not to write or make too much before verifying it works.