Here is the code for my game control -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameControl : MonoBehaviour
{
[SerializeField]
private GameObject target;
[SerializeField]
private Texture2D cursorTexture;
private Vector2 cursorHotspot;
private Vector2 mousePos;
[SerializeField]
private Text getReadyText;
[SerializeField]
private GameObject resultsPanel;
[SerializeField]
private Text scoreText, targetsHitText, shotsFiredText, accuracyText;
public static int score, targetsHit;
public float shotsFired;
public float accuracy;
private int targetsAmount;
private Vector2 targetRandomPosition;
// Start is called before the first frame update
void Start()
{
cursorHotspot = new Vector2(cursorTexture.width / 2, cursorTexture.height / 2);
Cursor.SetCursor(cursorTexture, cursorHotspot, CursorMode.Auto);
getReadyText.gameObject.SetActive(false);
targetsAmount = 50;
score = 0;
shotsFired = 0;
targetsHit = 0;
accuracy = 0f;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
shotsFired += 1f;
}
}
private IEnumerator GetReady()
{
for (int i = 3; i >= 1; i--)
{
getReadyText.text = "Get ready!/n" + i.ToString();
yield return new WaitForSeconds(1f);
}
getReadyText.text = "Go!";
yield return new WaitForSeconds(1f);
StartCoroutine("SpawnTargets");
}
private IEnumerator SpawnTargets()
{
getReadyText.gameObject.SetActive(false);
score = 0;
shotsFired = 0;
targetsHit = 0;
accuracy = 0;
for (int i = targetsAmount; i >= 0; i--)
{
targetRandomPosition = new Vector2(Random.Range(-7f, 7f), Random.Range(-4f, 4f));
Instantiate(target, targetRandomPosition, Quaternion.identity);
yield return new WaitForSeconds(1f);
}
resultsPanel.SetActive(true);
scoreText.text = "Score: " + score;
targetsHitText.text = "Targets Hit: " + targetsHit + "/" + targetsAmount;
shotsFiredText.text = "Shots Fired: " + shotsFired;
accuracy = targetsHit / shotsFired * 100f;
accuracyText.text = "Accuracy: " + accuracy.ToString("N2") + " %";
}
public void StartGetReadyCoroutine()
{
resultsPanel.SetActive(false);
getReadyText.gameObject.SetActive(true);
StartCoroutine("GetReady");
}
}
And this is the code for my target
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Target : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Destroy(gameObject, 1f);
}
// Update is called once per frame
void Update()
{
GameControl.score += 10;
GameControl.targetsHit = +1;
//Destroy(gameObject);
}
}
I cant get the target to ever destroy on my mouse click, please help.