Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collider that Changes Score but does not get Destroyed on Collision

Discussion in 'Scripting' started by BRIK1111, Sep 22, 2017.

  1. BRIK1111

    BRIK1111

    Joined:
    Sep 22, 2017
    Posts:
    2
    Hey guys, stupid question alert (at least it feels like it will be obvious).

    I've been messing around with the Space Shooter tutorial and want to add a collider that removes a couple of points if you miss any asteroids or enemy ships. I have a cube, placed just before the boundary, that's a trigger.

    Attached to the cube is this script:

    Code (CSharp):
    1. {
    2.     public int scoreValue;
    3.     private GameController gameController;
    4.  
    5.     private void Start()
    6.     {
    7.         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
    8.         if (gameControllerObject != null)
    9.         {
    10.             gameController = gameControllerObject.GetComponent<GameController>();
    11.         }
    12.         if (gameController == null)
    13.         {
    14.             Debug.Log("Cannot find 'Game Controller' script");
    15.         }
    16.     }
    17.  
    18.     private void OnTriggerEnter(Collider other)
    19.     {
    20.         if (other.gameObject.CompareTag("Enemy"))
    21.         {
    22.             gameController.AddScore(scoreValue);
    23.         }
    24.     }
    25. }
    The idea is that as soon as any stray enemies hit the cube, the gameController script with the AddScore code is called and the OnTriggerEnter function will add or minus the set amount (which I set to -2). However, while the logic works and the score subtracts, the cube gets completely destroyed after the first enemy hits it. Is there a way so that the enemies can keep flying through it without there being any physical collision?

    Thanks.
     
  2. PVisser

    PVisser

    Joined:
    Apr 24, 2014
    Posts:
    61
    I'm no expert myself so take my advice with a grain of salt.

    There doesn't seem to be any 'destroy' code in the script you provided, therefor I suspect your enemy ship script is destroying objects with other colliders instead of specific ones. Maybe you have your score cube too close to your 'destroyer' boundary which destroys it after some time, or the enemy ship pushes the cube into that boundary?

    I assume you have set your score cube as a trigger and not a collider with physical properties.
     
    BRIK1111 likes this.
  3. BRIK1111

    BRIK1111

    Joined:
    Sep 22, 2017
    Posts:
    2
    You hit the nail on the head, SylarKnight! Thank you. The destroy code was indeed attached to the enemies, although there were exceptions included (the boundary and the other enemies) so it was just a case of adding my trigger cube to a tag and then adding that to the code:

    Code (CSharp):
    1. if (other.CompareTag("Boundary") || other.CompareTag ("Enemy") || other.CompareTag("NegativeScore"))
    2.         {
    3.             return;
    4.         }
    Very simply. Thanks again.
     
    PVisser likes this.