I’m having problems keeping score when destroying objects when they are clicked. I think its because of the clones I’m generating.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Touch : MonoBehaviour {
public GameObject pinnedRing;
RaycastHit2D hitInfo;
public int score;
void Start()
{
score = 0;
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
//Debug.Log ("Clicked");
Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
if(hitInfo.collider.tag == "Ring")
{
score += 1;
Destroy (hitInfo.collider.gameObject);
Vector3 pinPos = hitInfo.transform.position;
GameObject secondRing = Instantiate (pinnedRing, pinPos, transform.rotation);
Destroy(secondRing, 1f);
}
if(hitInfo.collider.tag != "Ring")
{
//Do nothing
}
}
Debug.Log (score);
}
}
Basically, I spawn the objects and when I click them they disappear, but the score doesn’t add, it just keeps on 0. How can I fix this?