Hi,
I tryed a alternative solution for the pinSet instantiation and that was use Resource.Load (“path”) to instantiate the pin set from prefab without creat a public variable.
But it didn’t work and I don’t know why. Here is the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using AOT;
using System.Diagnostics;
public class PinSetter : MonoBehaviour
{
private int lastStandingCount;
private float lastStandingTime;
private bool ballInTheBox;
private GameObject pinCountUI;
private Ball ball;
privateGameObject pinSet;
// Use this for initialization
void Start ()
{
lastStandingCount = -1;
pinCountUI = GameObject.Find (“PinCountUI”);
ballInTheBox = false;
ball = GameObject.FindObjectOfType ();
// Also "Pins" here pinSet = Resource.Load ("Prefabs/Pins") as GameObject;
}
// Update is called once per frame
void Update ()
{
if (ballInTheBox) {
CountStanding ();
CheckStanding ();
}
}
//Count standing pins
public int CountStanding ()
{
int pinCounter = 0;
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()) { if (pin.IsStanding ()) { pinCounter++; } } pinCountUI.GetComponent<Text> ().text = pinCounter.ToString ("D2"); return pinCounter;
}
//Check if the ball is the pinsetter box
void OnTriggerEnter (Collider collider)
{
if (collider.GetComponent ()) {
pinCountUI.GetComponent ().color = Color.red;
ballInTheBox = true;
}
}
//Destroy pins when they exit pinsetter
void OnTriggerExit (Collider collider)
{
if (collider.GetComponentInParent ()) {
DestroyObject (collider.transform.parent.gameObject);
}
}
void CheckStanding ()
{
int count = CountStanding ();
if (count != lastStandingCount) {
lastStandingTime = Time.time;
lastStandingCount = CountStanding ();
return;
}
if (Time.time - lastStandingTime > 5) {
PinsHaveSettled ();
}
Invoke ("PinsHaveSettled", 3);
}
void PinsHaveSettled ()
{
lastStandingCount = -1;
ballInTheBox = false;
pinCountUI.GetComponent<Text> ().color = Color.green;
ball.Reset ();
}
public void RaisePins ()
{
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()) {
pin.Raise ();
}
}
public void LowerPins ()
{
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()) {
pin.Lower ();
}
}
public void RenewPins ()
{
ball.Reset ();
Instantiate (pinSet, new Vector3 (0, 0, 1828), Quaternion.identity);
}
}