Strange number of pins in the output

Hello, I am having problem with counting pins. When I call the CountPins() method in Start, there is correct number - 10. But when I call it in update, it keeps showing me seemingly random number between 0 and 10:

My guess is that it keeps calling it from the Update function over and over, but since return is at the bottom, after for each loop, shouldn’t it still be always 10?

public class PinSetter : MonoBehaviour {

private Pin[] pins;
private Text amountOfPinsText;

void Start () {
	amountOfPinsText = GameObject.FindObjectOfType<Text>();
}

void Update () {
		int pinsStanding = CountStandingPins();
		amountOfPinsText.text = pinsStanding.ToString();
		print (CountStandingPins());
}

int CountStandingPins () {
	int standingPinsNumber = 0;

		foreach (Pin pinObject in GameObject.FindObjectsOfType<Pin>()) {

			if(pinObject.IsStanding()){
				standingPinsNumber ++;
			}
		}
		return standingPinsNumber;
}
}

public class Pin : MonoBehaviour {

public float standingThreshold = 3f;


    public bool IsStanding () {

    	Vector3 rotationInEuler = transform.rotation.eulerAngles;

    	float tiltInX = Mathf.Abs (rotationInEuler.x);
    	float tiltInZ = Mathf.Abs (rotationInEuler.z);

    	if (tiltInX < standingThreshold && tiltInZ < standingThreshold) {
    		return true;
    	}else {
    	return false;
    	}
    }
    }

Where is the mistake? Please help:).

Does this happen after the ball hits the pins?

Maybe it’s because the pins that fall from the lane floor aren’t destroyed, so sometimes, during the fall, they go back in a standing position while falling, since falling doesn’t prevent the pins from rotating.

No, sorry, I forgot to mention that, but it’s before the ball is even launched.

Oops, my bad, I didn’t notice that you’re still using the Mathf.Abs method, since from Unity 5.3 rotation angles never have a negative value you need to change the IsStanding method as suggested in this topic:

8 Likes

Thank you, that completely solves the problem:). Now I can move on with the course.

Thanks everyone, this solved my problem too.

I had this problem too, but I thought it was okay and moved on until I stuck in counting scores.
Thanks everyone, this solved my problem perfectly!

Privacy & Terms