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:).