Need Help With Bowling Hit Count

Hello there, firstly, sorry for my English, it’s not my native language. I’m currently working on a bowling game for my thesis, and i am not able to count hits in a bowling game. I was working in c# with “void OnCollisionEnter” method,

using UnityEngine;
using System.Collections;

public class KeepScore : MonoBehaviour {

	Vector3 defpos;
	Vector3 defrot;
	Vector3 defangle;
	bool isCollidedWithPin = false;
	bool isCollidedWithSphere = false;

	// Use this for initialization
	void Start () {
		defpos = transform.position;
		defrot = transform.rotation.eulerAngles;
		defangle = transform.eulerAngles;
	}
	
	// Update is called once per frame
	void Update () {
		Debug.Log (MainScore.mainScore);
	}

	void OnCollisionEnter(Collision c){
		if (isCollidedWithSphere == false  c.transform.name.Equals("Pin")){
			isCollidedWithPin = true;
			StartCoroutine(StartWait());
		}
		else if (isCollidedWithPin == false  c.transform.name.Equals("Sphere")){
			isCollidedWithSphere = true;
			StartCoroutine(StartWait());
		}
	}

	void reset(){
		transform.position = defpos;
		transform.eulerAngles = defangle;
		transform.rotation.eulerAngles.Set(defrot.x,defrot.y,defrot.z);
		isCollidedWithPin = false;
		isCollidedWithSphere = false;
	}

	IEnumerator StartWait(){	
		yield return new WaitForSeconds (3); 
		if ((defrot.z != transform.rotation.eulerAngles.z || defrot.x != transform.rotation.eulerAngles.x)) {
			MainScore.mainScore++;
			reset ();
		}else{
			reset ();
		}
	}
}

How can i keep hit count? It drives me crazy, in which part i’m wrong? Your help will be very appreciated.

Hello ugrkucuk,
Have you attached a ridgidbody to the bowling ball?
You check the collision by the name of the transform/gameObject (line 25 and line 29).
If you have not a ridgidbody on the correct object, your c.transform will be the transform of your bowling ball and not that of the pin.
Try to get the right order.

Yes, both pins and ball have rigidbody, my problem is kinda different here. In my scene i have four pins, i hit them with ball and they also collide with each other, because of that, score variable is increased twice, instead of 4, i get 6. How can i prevent it?

Don’t add to the score as you go; calculate it on the fly. Each bowling pin should have a flag that says “I’ve been hit”. Is there a script on your pins? If not, create one, and do something like:

public bool hasBeenHit = false;

public void ResetPin() {
// whatever pin reset logic goes here
hasBeenHit = false;
}
void OnCollisionEnter(Collosion c) {
//perhaps "if it's not the floor" logic should go here
hasBeenHit = true;
}

....

//In your master score-keeping script
public BowlingPin[] allPins; //either assigned in the inspector, or set in script when the pins are created

public int CountScore() {
int score=0;
for (int p=0;p<allPins.Length;p++) {
if (allPins[p].hasBeenHit) score++;
}
return score;
}

Alternative technique: Don’t track collisions at all, but simply assume that any pin that isn’t straight up should be scored. No on-pin script needed for this one:

public Transform[] allPins; //either assigned in the inspector, or set in script when the pins are created

public int CountScore() {
int score=0;
for (int p=0;p<allPins.Length;p++) {
Vector3 pinsUpVector = allPins[p].up;
if (Vector3.Angle(pinsUpVector, Vector3.up) > 1f) score++;
}
return score;
}

Second option is simpler, and won’t score pins that are still right-side-up after being hit (which is how traditional bowling is scored anyway, if I’m not horribly mistaken).

Your post was very helpful, it solved my problem, thank you, really!

@StarManta I tried the 2nd option you recommended but I am not getting any score.Can u pls help ?