Scoring and player stats:

Hi all.

Here is where I got to.

I added a multiplier to the IncrementScore so that the DetectScore could pass in a unique amount.

ScoreKeeper.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ScoreKeeper : MonoBehaviour {

    int score = 0;
    
    int hitCount = 0;
    
    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {

        int balls = BallLauncher.ballsShot;

        if (Input.GetKeyDown(KeyCode.P))
        {
            double hitAverage = Math.Round((((float)hitCount / (float)balls) * (float)100), 2);
            print("Player Stats:\nScore: " + score + ".     Hits: " + hitCount + " / " + BallLauncher.ballsShot + ".     Hit Average: " + hitAverage + " %.");
        }
    }

    public void IncrementScore(int multiplier)
    {
        score = score + multiplier;
        print("Score: " + score);
    }
    public void IncrementHitCount()
    {
        hitCount++;
    }
}

I then made a public scoreMultiplier field in the DetectScore.cs so that the Unity user can dial in per prefab what score they get.

DetectScore.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DetectScore : MonoBehaviour {

    public int scoreMultiplier = 1;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    void OnCollisionEnter(Collision collision)
    {
        ScoreKeeper scoreKeeper = FindObjectOfType<ScoreKeeper>();
        scoreKeeper.IncrementScore(scoreMultiplier);
        scoreKeeper.IncrementHitCount();
    }
}

I have also added in player stats in the ScoreKeeper. This is so if you press the P key you get your current score, the amount of hits vs balls thrown and the percentage of that.

Privacy & Terms