Doing the scoring in a different way

Hi, I’d like to share my solution to the scoring mechanics.
NOTE that everything that is not mentioned here stays the same.

In my version of the game, I added a few different enemy types with different stats and difficulties, so I wanted to reward the player accordingly.

  1. Instead of putting the score value and the isPlayer check on a health script, I made a new one, called: PointsEvaluator.
    This component is simple: It has:
  • [SerializeField] int pointsValue = 10; Ammount of points you will get after destroying a different enemy.
 public int GetPointsValue()
    {
        return pointsValue;
    }

A method that just returns the value of the points.

  1. I added this component to the enemy prefabs and entered the score for each one of them.

  2. In Health.cs, in Die() method I call another private method called AddPoints()

  • AddPoints gets the reference to the component of type PointsEvaluator, but before that, it is initialized to null. PointsEvaluator pointsEvaluator = null;
  • After that, I used a new workflow that wasn’t covered so far in the course (I know of this because of my previous programming experiences). It is using try and catch keywords.
    What they do essentially is telling the compiler to try and do a thing, and if it fails, that is: if we were to get some type of error in the console, it allows us to catch that error and handle it or do something else.
    There I made it so that the try {} block tries to set the pointsEvaluator, as: pointsEvaluator = gameObject.GetComponent<PointsEvaluator>();
    And the catch (Exception e) {} if there is an exception, that is if the game fails to find the PointsEvaluator on a gameObject, specifically: when the player gets destroyed as it doesn’t have that component. An there I simply return out of the method.
    A small NOTE is that catch part must have that (Exception e) as this is the way it will accept the error and then we will be able to handle it.
    Errors of the type Exception are from the system namespace, so we must include it at the top of our script: using System;
    Lastly, i check if my pointsEvaluator variable is different from null and then if it is, I add the score through our score keeper: scoreKeeper.AddPoints(pointsEvaluator.GetPointsValue());

Full AddPoints method:

   void AddPoints()
    {
        PointsEvaluator pointsEvaluator = null;
        try
        {
            pointsEvaluator = gameObject.GetComponent<PointsEvaluator>();
        }
        catch (Exception e)
        {
            return;
        }
        if (pointsEvaluator != null)
        {
            scoreKeeper.AddPoints(pointsEvaluator.GetPointsValue());
        }
    }```
2 Likes

This is the type of detailed post I love to see. Not only did you showcase that you did the scoring a different way which is awesome btw :clap: but also you went into depth how you did it and your thought process. This really helps me get into your mindset and help me understand how to do it if I wanted to add this version of keeping score. Thank you for this :pray:t3:

1 Like

Thank you so much for your reply.
I’m glad that you liked this and if you were also able to understand it, it makes me even happier.

I always try to see if there is a different and better approach to the given problem/solution.
It not only helps me learn more but also understand more and be more aware of the possibilities for my future projects that I have in mind.
I’d recommend to everyone that for every game in course, they try to add some more functionallities, and don’t be afraid to make it complicated. Push yourself as much as possible.

Best wishes to you in your learning and gamemaking!

Hello, Shone95,

I think this is a very good solution, but the try catch on the GetComponent<> is not necessary, because this function returns the relevant component, or a NULL if it cannot find it, but no Exception.
So that’s why the try catch is of no use here.

Privacy & Terms