Adding a score when a rigid body hits another

Hi, I am new to unity and I am developing a simple 2D bowling game. Now I have run into a problem and cannot seem to find an answer to it online. I need to make a scoreboard that add a score for every pin that has been dropped, that is either if the bowling ball hits the pins or if the pins hit themselves. Can anyone help me with this? thx

I think that you can simply do a check if the pins are still in their position or not rather than force yourself to write methods to check for collisions (OnCollision is easy to implement, but it seems more complicated to understand if the collision did it fall or not). In particular:

  • If you have an array (or list) of your GameObject-pins, then scroll and compare with the initial positions (that are known in this game).
  • Or in a coroutine, after a certain tot of seconds check the object rotation (if has changed is dropped).

First create a collider script. This script should be attached to every pin. And after that you should drag and drop your GuiText to ScoreText component in Inspector Window.

collider.js

var ScoreText : GameObject;

function OnTriggerEnter(){
	ScoreText.gameObject.SendMessage("scoreUpdate");	    
}

You need to make box collider which “Is Trigger” option is checked. If your pin has already a collider and if you check “Is Trigger” option this time ball will pass throught the pins. So you can make two colliders for each pin. One of them should be “Is Trigger” option is checked.This script will send message to your scoreText script. For example if the bowling ball hit the pin collider script tell scoreText script to new score. Ok we need a scoreText script then.

ScoreText.js

//Attach to GuiText Game Object

var score : int;
var newScore : int = 100;

function Start () {

}

function Update () {

guiText.text = "Score : " + score;
}

function scoreUpdate () {
	score = score + newScore;
	}

This script should be attached to the GuiText game object. (GameObject/Create Other/GUI Text)

You can also check that either if the bowling ball hits the pins or if the pins hit themselves. To do this use tags for your pins and ball.

var ScoreText : GameObject;
	
function OnTriggerEnter(pinCollider : Collider){
	if(pinCollider.gameObject.tag == "Ball"){
    	ScoreText.gameObject.SendMessage("scoreUpdate"); 
	}   
}

Thats it. Good luck :slight_smile: