Can't count the hit bowling pins[physics in 3d]

Hi all,
I am still new to Unity and trying to create my own bowling game. I have a bowling ball with a rigidbody attached to it and a bowling pin prefab with 10 pins attached to it (tagged pin). My floor is tagged “floor”. Right now I have one script for the ball and I’m trying to create a second one, that should be attached to the pins which will check if they have been hit or not. I find this quite difficult even though I have the feeling it is not as complicated as I make it.

public class Pins : MonoBehaviour {

       public int score;
           public int pinsNumber = 10;
       
           void OnCollisionEnter(Collision other)
       {
	      if(other.gameObject.CompareTag("floor"))
	      {
		      for (int i = 0; i <= pinsNumber; i++)
		      {

			      if(Vector3.Dot(transform.up, other.transform.up) < 0.9f)
			      {
				     // Increase score here
				        score+=1;
				        int total = score;
				        Debug.Log (total);
			      }
		      }

	      }

      }
	


    }

Basically, what this is doing now is the Console outputs increasing numbers if I hit the pins. Any help will be appreciated.

Attach this script to your bowling ball:

public class BowlingBall : MonoBehaviour {
    
	public bool collided = false;
	
	void OnCollisionEnter(Collision other){
		
		//checking if the bowling ball has collided with pins, not the floor
		if (other.gameObject.tag == "pin" && !collided){
			collided = true; // prevents the wait function from running multiple times
			StartCoroutine(Wait());
		}
	}
	
	public IEnumerator Wait(){
		// wait for 5 seconds (the f makes the number we enter a float)
		yield return new WaitForSeconds(5.0f);
		
		// looping through each pin prefab and calling the tallyScore function
		GameObject[] pins = FindObjectsOfType(typeof(GameObject)) as GameObject[];
        foreach (GameObject pin in pins) {
            pin.GetComponent<Pins> ().tallyScore();
        }
	}
}

Attach this script to your pin prefab:

public class Pins : MonoBehaviour {
    
	public static int score = 0;

	public void TallyScore(){
		
		//checking if the pin is standing up (if it's not, the pin's rotation will be not 0)
		if (transform.rotation.x < 1 && transform.rotation.z < 1){
			//pin is standing up (do nothing)
		} else {
			//pin is knocked over
			score++;
		}
	}
}

This code basically checks if the bowling ball has hit a pin, waits 5 seconds, then checks if the pin is knocked over, and if it is, it adds one to the score.

Score is static so that each prefab doesn’t have its own separate score, they all share the one variable.

If you have any questions, do not hesitate to ask!

EDIT: NEW PIN SCRIPT TESTING

 public class Pins : MonoBehaviour {
 
     void Update(){
         
         if (transform.rotation.x < 1 && transform.rotation.z < 1){
             Debug.Log("Pin Standing Up");
         } else {
             Debug.Log("Pin Knocked Over");
         }
     }
 }

Try attaching this script to your pin prefab and turning it over onto its side while you watch the console.

Make sure that the changes you make to the pin are applied to the prefab.

Hello @n314p and thank you again.

I added the new script and now the whole time I have the message “Pin is Standing UP” . It doesn’t change regardless of the position of the pins. I have attached it to the pin prefabs so all Pin objects have the script.

Also, the nUllreferenceexception error is still there. I think I am not initializing the pins correctly. This is my Ball script.
using System.Collections.Generic;
using UnityEngine;

public class BallMovement : MonoBehaviour {

//force per one pixel
public float powerPerPixel;
public float maxPower;
public float sensitivity;
    public bool collided = false;
Vector3 touchPosition;
bool isMoving;

// Use this for initialization
void Start () 
{
	isMoving = false;

}

// Update is called once per frame
void Update ()
{
	if (!isMoving) 
	{
		if (Input.GetMouseButtonDown (0))
		{
			touchPosition = Input.mousePosition;
		}
		else if (Input.GetMouseButtonUp (0)) 
		{
			isMoving = true;
			transform.Rotate (new Vector3 (1, 5, 10) * Time.deltaTime);
			Vector3 releasePosition = Input.mousePosition;

			float swipeDistanceY = releasePosition.y - touchPosition.y;
			float power = swipeDistanceY * powerPerPixel;
			float swipeDistanceX = releasePosition.x - touchPosition.x;
			float throwDirection = swipeDistanceX * sensitivity;

			if (power < 0) {
				power *= -1;
			}

			if (power > maxPower) {
				power = maxPower;
			}

			GetComponent<Rigidbody> ().AddForce (new Vector3 (throwDirection, 0, power), ForceMode.Impulse);
		}

	}
			
}


void OnCollisionEnter(Collision other)
{
	if (other.gameObject.tag == "pin" & !collided) {
		collided = true;
		StartCoroutine (Wait());
	}
}

public IEnumerator Wait()
{
	yield return new WaitForSeconds (5.0f);

	GameObject[] pins = FindObjectsOfType (typeof(GameObject)) as GameObject[];
	//GameObject[] pins = GameObject.FindGameObjectsWithTag("pin") as GameObject[];
	foreach (GameObject pin in pins) {
		pin.GetComponent<Pins> ().TallyScore ();
	}
	Debug.Break ();
}

}

Also, I noticed that when my ball collides with the pins, the z value stays higher than 1.

My pins script is the same as you suggested.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pins : MonoBehaviour {

public static int score = 0;

void Update()
{
	if (transform.rotation.x < 1 && transform.rotation.z < 1) {
		Debug.Log ("Pin Standing Up");
	} else {
		Debug.Log ("Pin Knocked Over");
	}

}

public void TallyScore()
{
	if (transform.rotation.x < 1 && transform.rotation.z < 1) {

	} else {
		score++;
	}
}

}

to calculate the bowling score accuratly, counting of bowling pins is essential in the game. go bowling alley