I have an object that spawns the coin prefab and this is the script attached to the prefab. When i click on the coins they are destroyed properly, but I receive the value of all active coins at the time I click any of them.
Here is my Script.
using UnityEngine;
using System.Collections;
public class Token : MonoBehaviour
{
private GameObject Coin;
private Money mscr;
public float Worth;
private Mine minescr;
// Use this for initialization
void Start ()
{
mscr = GameObject.Find("GameLogic").GetComponent<Money> ();
minescr = GameObject.Find("Mine").GetComponent<Mine> ();
}
void CollectCoin()
{
mscr.money += Worth;
Debug.Log("Cha-ching");
minescr.hasToken = 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.tag == "Coin")
{
Coin = hit.transform.gameObject;
}
else
{
Coin = null;
}
}
if (Input.GetMouseButtonDown(0) && Coin != null)
{
CollectCoin();
}
}
}