hi
I am having 2 issues with the Pinsetter code at this point.
- when playtesting game I get this : " NullReferenceException: Object reference not set to an instance of an object
PinSetter.Update () (at Assets/Scripts/PinSetter.cs:28)"
yet I have same as Ben yet he doesn’t get this Null Refrence
- I have animations working great for tidy and reset while using buttons. But I cannot get script to actvate animations for tidy or reset.
the spelling is exactly the same for both so I know that is not the issue.
- View does not always reset to beginning view of lane.
Michael
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PinSetter : MonoBehaviour {
public int lastStandingCount = -1;
public Text standingDisplay;
public float distanceToRaise = 1f;
public float distanceToLower = -1f;
public GameObject pinSet;
private Ball ball;
private float lastChangeTime;
private int lastSettledCount = 10;
private bool ballEnteredBox = false;
private ActionMaster actionMaster = new ActionMaster();
private Animator animator;
// Use this for initialization
void Start () {
ball = GameObject.FindObjectOfType<Ball> ();
animator = GetComponent<Animator> ();
}
void Update () {
standingDisplay.text = CountStanding ().ToString ();
if (ballEnteredBox) {
UpdateStandingCountAndSettle();
}
}
public void RaisePins () {
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()){
pin.RaiseIfStanding();
}
}
public void LowerPins () {
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()){
pin.Lower();
}
}
public void ResetPins () {
Instantiate (pinSet, new Vector3 (-5f, 20f, 1843f), Quaternion.identity);
}
void UpdateStandingCountAndSettle () {
// Update the standing count
// Call PinsHaveSettled() when they have
int currentStanding = CountStanding ();
if (currentStanding != lastStandingCount) {
lastChangeTime = Time.time;
lastStandingCount = currentStanding;
return;
}
float settleTime = 3f; //How ;long to settle time for pins settled
if ((Time.time - lastChangeTime) > settleTime) {
PinsHaveSettled();
}
}
void PinsHaveSettled () {
int standing = CountStanding ();
int pinFall = lastSettledCount - standing ();
lastSettledCount = standing;
ActionMaster.Action action = actionMaster.Bowl (pinFall);
if (action == ActionMaster.Action.Tidy) {
animator.SetTrigger ("tidyTrigger");
} else if (action == ActionMaster.Action.EndTurn) {
animator.SetTrigger ("resetTrigger");
} else if (action == ActionMaster.Action.Reset) {
animator.SetTrigger ("resetTrigger");
} else if (action == ActionMaster.Action.EndGame) {
throw new UnityException ("Don't know how to handle this");
}
ball.Reset ();
lastStandingCount = -1;
ballEnteredBox = false;
standingDisplay.color = Color.blue;
}
int CountStanding () {
int standing = 0;
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()){
if (pin.IsStanding()) {
standing++;
}
}
return standing;
}
void OnTriggerEnter (Collider collider) {
GameObject thingHit = collider.gameObject;
//Ball enters play box//
if (thingHit.GetComponent<Ball> ()) {
ballEnteredBox = true;
standingDisplay.color = Color.red;
}
}
}