Basically, what I’m asking is how do I make my coin script tell the mine script, of the mine that spawned the coin originally, that the coin was collected without telling all the mines that I collected a coin.
In our game we have multiple mines, each spawning it’s own coin. Once this happens a bool is set to tell the mine not to spawn another coin. When I collect a coin from any mine, the last one spawned is the only one affected by the bool change, that I am asking the Token script to change when collection has occurred. Which causes a stack of coins to appear.
Coin Script:
using UnityEngine;
using System.Collections;
public class Token : MonoBehaviour
{
private GameObject Coin;
private Money mscr;
public float Worth;
private MineScript minescr;
// Use this for initialization
void Start ()
{
mscr = GameObject.Find("GameLogic").GetComponent<Money> ();
minescr = GameObject.Find("Mine").GetComponent<MineScript> ();
}
void CollectCoin()
{
mscr.money += Worth;
Debug.Log("Cha-ching");
minescr.hasToken = false;
Debug.Log("Reset to false");
Destroy(Coin);
Debug.Log("Got collected");
}
// Update is called once per frame
void Update ()
{
transform.Rotate (Vector3.right * Time.deltaTime * 150);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 300))
{
if (hit.transform == transform)
{
Coin = hit.transform.gameObject;
}
else
{
Coin = null;
}
}
if (Input.GetMouseButtonDown(0) && Coin != null)
{
CollectCoin();
}
}
}
Mine Script:
using UnityEngine;
using System.Collections;
public class MineScript : MonoBehaviour
{
public float Cooldown;
private float cd;
public GameObject token;
public bool hasToken = false;
public GameObject Coin;
private Money mscr;
public float Worth;
public bool gotCollected = false;
// Use this for initialization
void Start ()
{
mscr = GameObject.Find("GameLogic").GetComponent<Money> ();
}
// Update is called once per frame
void Update ()
{
if (hasToken == false)
{
if (cd >= 0)
{
cd -= Time.deltaTime;
}
else
{
cd = Cooldown;
SpawnToken();
}
}
}
void SpawnToken ()
{
Vector3 pos = new Vector3(transform.position.x,6F,transform.position.z);
Instantiate(token,pos,Quaternion.identity);
hasToken = true;
}
}