Search Unity

Question Game over text not showning

Discussion in 'Scripting' started by vardhanyavaid1, Feb 12, 2024.

  1. vardhanyavaid1

    vardhanyavaid1

    Joined:
    Feb 7, 2024
    Posts:
    8
    So I am trying to make a game like fruit ninja, in which you basically click on a prefab using mouse and it destroys it, updating your score by 5. If you click on the bad prefab, you loose 5 points. If a good prefab falls on the sensor, an OnTriggerEnter method destroys the object and should run the gameOver Function, which should show a text Game Over. But the If statement that that triggers this is not working.

    The target Mover script:

    Code (CSharp):
    1. using JetBrains.Annotations;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class targetMover : MonoBehaviour
    8. {
    9.     private gameManager gameManagerScript;
    10.     private Rigidbody targetRb;
    11.     int minForce = 16;
    12.     int maxForce = 18;
    13.     int minTorque = -10;
    14.     int maxTorque = 10;
    15.     int minPosX = -4;
    16.     int maxPosX = 4;
    17.     int posY = -6;
    18.     public int pointValue;
    19.     public ParticleSystem explosionParticle;
    20.     public bool hasCollided = false;
    21.    
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         targetRb = GetComponent<Rigidbody>();
    26.         //Adding Force, torque and assigning position to GameObject
    27.         targetRb.AddForce(randomForce(), ForceMode.Impulse);
    28.         targetRb.AddTorque(randomTorque(), randomTorque(), randomTorque(), ForceMode.Impulse);
    29.         transform.position = randomPos();
    30.         gameManagerScript = GameObject.Find("gameManager").GetComponent<gameManager>();
    31.        
    32.        
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.        
    39.     }
    40.  
    41.     private void OnMouseDown()
    42.     {
    43.         gameManagerScript.updateScore(pointValue);
    44.         Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation);
    45.         Destroy(gameObject);
    46.     }
    47.  
    48.     private Vector3 randomForce()  //Generates a Random force and returns the random Vector
    49.     {
    50.         return Vector3.up*Random.Range(minForce, maxForce);
    51.     }
    52.  
    53.     private float randomTorque()  //Generates a Random Torque and returns the random float
    54.     {
    55.         return Random.Range(minTorque, maxTorque);
    56.     }
    57.  
    58.     private Vector3 randomPos()  //Generates a Random Force and returns the random Vector
    59.     {
    60.         return new Vector3(Random.Range(minPosX, maxPosX), posY);
    61.     }
    62.  
    63.     private void OnTriggerEnter(Collider other)
    64.     {
    65.  
    66.  
    67.         Destroy(gameObject);
    68.  
    69.         if (other.gameObject.CompareTag("Target"))
    70.         {
    71.             gameManagerScript.gameOverMethod();
    72.             Debug.Log("Object Fell");
    73.         }
    74.  
    75.  
    76.  
    77.  
    78.     }
    79. }
    80.  
    The game Management script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class gameManager : MonoBehaviour
    7. {
    8.    
    9.     public TextMeshProUGUI scoreText;
    10.     public List<GameObject> targets;
    11.     public int playerScore;
    12.     public TextMeshProUGUI gameOver;
    13.    
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.         playerScore = 0;
    19.         StartCoroutine(spawnTargets());
    20.         updateScore(0);
    21.        
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.        
    29.     }
    30.    
    31.    
    32.  
    33.     IEnumerator spawnTargets()
    34.     {
    35.        
    36.         while (true)
    37.         {
    38.             yield return new WaitForSeconds(1);
    39.             int index = Random.Range(0, targets.Count);
    40.             Instantiate(targets[index]);
    41.            
    42.         }
    43.        
    44.     }
    45.  
    46.     public void updateScore(int scoreToAdd)
    47.     {
    48.         playerScore += scoreToAdd;
    49.         scoreText.text = "Score:" + playerScore;
    50.     }
    51.  
    52.     public void gameOverMethod()
    53.     {
    54.         gameOver.gameObject.SetActive(true);
    55.     }
    56. }
    57.  
    Please help with this problem. I am new to unity
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,627
    First, find out if your OnTriggerEnter is actually being called. Put a Debug.Log statement just inside and see what happens when something hits the trigger collider.
     
  3. vardhanyavaid1

    vardhanyavaid1

    Joined:
    Feb 7, 2024
    Posts:
    8
    My onTriggerMethod is indeed being called, because the game objects get destroyed once they hit the sensor. I had put a Debug.Log Statement inside the if statement, but that didn't get logged in the console. That is not being called
     
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    712
    In this case it simply means that your other game object does not have the required tag (or it's misspelt). Instead of using Tags, just create MonoBehaviours acting like tags and add them to the object, this way you avoid using hard coded strings (aka magic strings), and you can add code inside (for example it's better to have the destroy code inside this script, to separate concerns):

    Code (CSharp):
    1. public class Fruit : MonoBehaviour
    2. {
    3.     // Set to true in the Inspector if this Fruit is a target (triggers a game over)
    4.     public bool isTarget;
    5.  
    6.     public void DestroyGameObject()
    7.     {
    8.         // maybe add some effects: sounds, particles...
    9.         Destroy(gameObject);
    10.     }
    11. }
    Then your trigger code would become:
    Code (CSharp):
    1. if (other.gameObject.TryGetComponent<Fruit>(out var fruit))
    2. {
    3.     Debug.Log($"Fruit {fruit} hit the trigger area");
    4.     fruit.DestroyGameObject();
    5.  
    6.     if (fruit.isTarget)
    7.     {
    8.         Debug.Log($"Game over! the fruit {fruit} was a target", fruit);
    9.         gameManagerScript.gameOverMethod();
    10.     }
    11. }
    12. else
    13. {
    14.     Debug.Log($"The object {other.gameObject} that hit the trigger area does not have a Fruit component attached");
    15. }