Adding value from a clone object to score

So I’m working on kind of a peggle esk project. The way it works is a projectile bounces around hitting pegs, every time it hits a peg it adds 100 to the score and the peg disapears. If it hits two pegs it adds 110 on the second one, 3 pegs it adds 120, and so on.

`public float value;
public float increase;
private float scoreM;
// scoreM is the score modifier, this is what will be added to the score at the end

void Start () {
	scoreM = value;

// the score value starts at just the base score before increasing
}

void OnCollisionEnter2D(Collision2D col)
{

	if (col.gameObject.tag == "peg") { 
		scoreM = scoreM + increase;

// every peg hit increases the scoreM by the increase value
}`

This works all fine and good, I have it printing the projectiles score value and it’s going up for sure. My problem is I need to keep track of the score. I would just keep track of it on the projectile, but it’s a clone, meaning every individual projectile will have it’s own score, and then when it disapears the score it has will disapear with it. So my question is, is it possible for me to have one public variable that all my scripts can acces? If not, can the projectile add to a seperate scripts public int?

Thanks for any assistance! Sorry if I didn’t give enough information, but any assistance is apreciated!

You could easily use a static float variable in a singleton.

Something like

public static class PlayerData
{
     public static int Score;
}

using that you’d just do PlayerData.Score to reference it from anywhere in the codebase.

but that design pattern is really ugly and has a large margin for human errors. The eligant solution to data storage and clean references across the project can be found here. Error

Or in your own research on ScriptableObjects - there are a lot of good references out there.

This simplest solution for this would be to use a static class member.

class ClassA{
static float score;
}

The static modifier means that ALL instances of ClassA will share the same single value for “score”.
If you make add the “public” modifier to the declaration of score,

class ClassA{
public static float score;
}

then you can access it from outside class A, like so:

ClassA.score += scoreIncrement;

Notice, you don’t need an instance of ClassA, you can simply use the class name itself to access the score variable.

Is this wat you want?

    public class Projectilescore : MonoBehaviour
    {
        public float value; 
        public float increase; 
        // scoreM is the score modifier, this is what will be added to the score at the end.
        private float scoreM;  
        
        void Start () 
        { 
            // the score value starts at just the base score before increasing.
            scoreM = value;  
        }

        void OnCollisionEnter2D(Collision2D col)
        {
            if (col.gameObject.tag == "peg")
            {
                // every peg hit increases the scoreM by the increase value.
                scoreM += increase;

                // Update the TotalScore:
                TotalScore.Score += increase;
            }
        }
    }

    public static class TotalScore
    {
        public static float Score { get; set; }
        
        static TotalScore()
        {
            Score = 0f;
        }

        public static void Reset()
        {
            Score = 0f;
        }
    }

Above we are using a static class… but you could also use a singleton to keep track of game scoring.

I added a Reset method in TotalScore so you can reset the value when restarting your game.

Thanks for all the help! The method I wen’t with was simply applying it to a static int “points” with a text command reading static int points. Thanks for the help!