Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Need Help with Destroying Targets on Click

Discussion in 'Scripting' started by cmc919, Apr 13, 2024.

  1. cmc919

    cmc919

    Joined:
    Apr 10, 2024
    Posts:
    2
    Here is the code for my game control -
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    Code (csharp):
    1.  
    2. public class GameControl : MonoBehaviour
    3. {
    4.     [SerializeField]
    5.     private GameObject target;
    6.  
    7.     [SerializeField]
    8.     private Texture2D cursorTexture;
    9.     private Vector2 cursorHotspot;
    10.     private Vector2 mousePos;
    11.  
    12.     [SerializeField]
    13.     private Text getReadyText;
    14.  
    15.     [SerializeField]
    16.     private GameObject resultsPanel;
    17.  
    18.     [SerializeField]
    19.     private Text scoreText, targetsHitText, shotsFiredText, accuracyText;
    20.  
    21.     public static int score, targetsHit;
    22.     public float shotsFired;
    23.     public float accuracy;
    24.     private int targetsAmount;
    25.     private Vector2 targetRandomPosition;
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         cursorHotspot = new Vector2(cursorTexture.width / 2, cursorTexture.height / 2);
    30.         Cursor.SetCursor(cursorTexture, cursorHotspot, CursorMode.Auto);
    31.  
    32.         getReadyText.gameObject.SetActive(false);
    33.  
    34.         targetsAmount = 50;
    35.         score = 0;
    36.         shotsFired = 0;
    37.         targetsHit = 0;
    38.         accuracy = 0f;
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.         if (Input.GetMouseButtonDown(0))
    45.         {
    46.          
    47.             shotsFired += 1f;
    48.         }
    49.     }
    50.  
    51.     private IEnumerator GetReady()
    52.     {
    53.         for (int i = 3; i >= 1; i--)
    54.         {
    55.             getReadyText.text = "Get ready!/n" + i.ToString();
    56.             yield return new WaitForSeconds(1f);
    57.         }
    58.         getReadyText.text = "Go!";
    59.         yield return new WaitForSeconds(1f);
    60.         StartCoroutine("SpawnTargets");
    61.  
    62.     }
    63.  
    64.     private IEnumerator SpawnTargets()
    65.     {
    66.         getReadyText.gameObject.SetActive(false);
    67.         score = 0;
    68.         shotsFired = 0;
    69.         targetsHit = 0;
    70.         accuracy = 0;
    71.         for (int i = targetsAmount; i >= 0; i--)
    72.         {
    73.             targetRandomPosition = new Vector2(Random.Range(-7f, 7f), Random.Range(-4f, 4f));
    74.             Instantiate(target, targetRandomPosition, Quaternion.identity);
    75.  
    76.             yield return new WaitForSeconds(1f);
    77.         }
    78.         resultsPanel.SetActive(true);
    79.         scoreText.text = "Score: " + score;
    80.         targetsHitText.text = "Targets Hit: " + targetsHit + "/" + targetsAmount;
    81.         shotsFiredText.text = "Shots Fired: " + shotsFired;
    82.         accuracy = targetsHit / shotsFired * 100f;
    83.         accuracyText.text = "Accuracy: " + accuracy.ToString("N2") + " %";
    84.     }
    85.     public void StartGetReadyCoroutine()
    86.     {
    87.         resultsPanel.SetActive(false);
    88.         getReadyText.gameObject.SetActive(true);
    89.         StartCoroutine("GetReady");
    90.     }
    91. }
    92.  

    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.
     
    Last edited: Apr 13, 2024
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Where in your code are you trying to destroy a target on a mouse click?
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,544
  4. cmc919

    cmc919

    Joined:
    Apr 10, 2024
    Posts:
    2
    Oh my bad I accidentally deleted it but Im trying to destroy the target in update in game control when my mouse cursor is over the target and left click is down