So i was making an Aimlab mockup and made a counter variable to store how much target i clicked in the span of time i had. The game is pretty much done, so when i clicked one of the target it will move to another position (look at SpawnManager.cs). But when i played the game, clicked start, and then shot (clicked) on the target it seems like the counter was increased according to the amount of target i had on the scene, meanwhile i only want to increase it by one, hence why the name counter.
Is there anyone that could help me debug this? Thank you in advance.
These are my codes:
*Target.cs (game mechanic)*
public class Target : MonoBehaviour
{
[SerializeField] Camera cam;
[SerializeField] GameManager gameManager;
[SerializeField] int count;
private void Start()
{
cam = Camera.main.GetComponent();
gameManager = GameObject.Find("GameManager").GetComponent();
}
void Update()
{
if (gameManager.isGameActive) {
if (Input.GetMouseButtonDown(0)) {
Shoot();
}
}
}
void Shoot()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
if (Physics.Raycast(ray,out RaycastHit hit)) {
Target target = hit.collider.gameObject.GetComponent();
if (target != null) {
target.Hit();
}
}
}
void Hit()
{
transform.position = SpawnManager.instance.GetRandomPos();
gameManager.UpdateCounter(count);
}
}
*GameManager.cs (UI stuff)*
public class GameManager : MonoBehaviour
{
public TextMeshProUGUI counterText;
public TextMeshProUGUI timerText;
public TextMeshProUGUI gameOverText;
public Image crosshair;
public GameObject titleScreen;
public Button restartButton;
public Button playButton;
private int counter;
private float time = 10f;
public bool isGameActive;
private void Start()
{
playButton.onClick.AddListener(StartGame);
}
void Update()
{
if (isGameActive) {
if (time > 0) {
time -= Time.deltaTime;
timerText.text = "Timer: " + Mathf.Round(time);
}
else {
GameOver();
}
}
}
public void StartGame()
{
isGameActive = true;
counter = 0;
UpdateCounter(counter);
crosshair.gameObject.SetActive(true);
counterText.gameObject.SetActive(true);
timerText.gameObject.SetActive(true);
titleScreen.SetActive(false);
}
public void UpdateCounter(int count)
{
counter += count;
counterText.text = "Counter: " + counter;
}
// Stop game, bring up game over text and restart button
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
restartButton.gameObject.SetActive(true);
crosshair.gameObject.SetActive(false);
counterText.gameObject.SetActive(false);
timerText.gameObject.SetActive(false);
isGameActive = false;
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
*SpawnManager.cs (spawn target inside the box collider boundary)*
public class SpawnManager : MonoBehaviour
{
public static SpawnManager instance;
[SerializeField] BoxCollider boundCol;
[SerializeField] GameObject target;
void Awake()
{
instance = this;
}
public Vector3 GetRandomPos()
{
Vector3 center = boundCol.center + transform.position;
float minX = center.x - boundCol.size.x/2f;
float maxX = center.x + boundCol.size.x/2f;
float minY = center.y - boundCol.size.y/2f;
float maxY = center.y + boundCol.size.y/2f;
//float minZ = center.z - boundCol.size.z/2f;
//float maxZ = center.z + boundCol.size.z/2f;
float randX = Random.Range(minX, maxX);
float randY = Random.Range(minY, maxY);
//float randZ = Random.Range(minZ, maxZ);
Vector3 randPos = new Vector3(randX, randY, target.transform.position.z);
return randPos;
}
}
And this what the game looks like, as soon as i hit the middle target, the counter was increased by 3 and not 1![alt text][1]
[1]: /storage/temp/199919-screenshot-260-min.jpg
↧