Search Unity

2D Object not being destroyed by Collision script

Discussion in 'Scripting' started by kentg1, Jan 27, 2020.

  1. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    The Tripleshot object will hit the player and stop when i uncheck isTrigger on the Box Collider2D. When I check it the Triple Shot will go right through it and not be destroyed?

    On the Player Spaceship

    BoxCollider2d with Trigger
    Player tagged as "Player"


    On the Triple Shot

    BoxCollider2D with trigger
    Rigidbody2D with no gravity
    Powerupscript



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Powerup : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     float speed = 3.01f;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         transform.Translate(Vector3.down * speed * Time.deltaTime);
    21.  
    22.  
    23.         if (transform.position.y < -4.6f)
    24.         {
    25.  
    26.             Destroy(this.gameObject);
    27.  
    28.         }
    29.  
    30.     }
    31.  
    32.  
    33.     private void OntriggerEnter2d(Collider2D other)
    34.     {
    35.  
    36.         if (other.tag == "Player")
    37.  
    38.         {
    39.             Destroy(this.gameObject);
    40.         }
    41.  
    42.     }
    43.  
    44. }
    45.  
    46.  
    Any ideas?
    Thank you,
     

    Attached Files:

  2. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    It seems like it should work to me, but you can get rid of the "this," since "gameObject" refers directly to the game object the script is attached to.
     
  3. Deleted User

    Deleted User

    Guest

    It doesn't; the trigger is not registered, for some reason.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Powerup : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     float speed = 3.01f;
    7.  
    8.     private void Update()
    9.     {
    10.         transform.Translate(Vector3.down * speed * Time.deltaTime);
    11.  
    12.         if(transform.position.y < -4.6f)
    13.             Destroy(gameObject);
    14.     }
    15.  
    16.     private void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if(other.gameObject.CompareTag("Player"))
    19.         {
    20.             Debug.Log(other.tag);
    21.             Destroy(gameObject);
    22.         }
    23.     }
    24. }
     
    Last edited by a moderator: Jan 27, 2020
  4. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    I had a similar problem which I posted in "physics" yesterday. I followed the collision chart to a tee. For what it's worth, I restarted the engine, and now it works. Maybe you'll be as lucky.
     
  5. Deleted User

    Deleted User

    Guest

    No luck for me. :(

    I'd really like to know what the problem is, why the trigger is not registered.
     
  6. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    Any idea on how to fix it? It does collide with the object if I uncheck is trigger but is simply not destroying it.
     
  7. Deleted User

    Deleted User

    Guest

    It doesn't even collide for me...
     
  8. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    I restarted the project and no joy.
     
  9. Deleted User

    Deleted User

    Guest

    What version of Unity are you using?

    I'm testing with 2020.1.0a20.
     
  10. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    Have you tryed using OnCollisionEnter2D instead of trigger?
     
  11. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    Also a thing to try is to add a rigidbody to both objects with colliders, because a collider on a gameobject without a rigidbody is considered static.
     
  12. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    Just tried it (OnCollisionEnter2D) but did not work either

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Powerup : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     float speed = 3.01f;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         transform.Translate(Vector3.down * speed * Time.deltaTime);
    21.  
    22.  
    23.         if (transform.position.y < -4.6f)
    24.         {
    25.  
    26.             Destroy(gameObject);
    27.  
    28.         }
    29.  
    30.     }
    31.  
    32.  
    33.     private void OnCollisionEnter2D(Collider2D other)
    34.     {
    35.  
    36.         if (other.tag == "Player")
    37.  
    38.         {
    39.             Destroy(gameObject);
    40.         }
    41.  
    42.     }
    43.  
    44.  
    45. }
     
  13. Deleted User

    Deleted User

    Guest

    Tried that: no result.
    Tried that too...
     
  14. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    Two rigidbodys causes the powerup to simply push the player down,
     
  15. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    2019.3
     
  16. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    So this script does have the enemy being destroyed when it hits the player, and the bullets destroying the enemy.. And it is exactly like the Powerip script and the same player is hitting both with the exact same physics in it .

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Destoyenemy1 : MonoBehaviour
    6. {
    7.     public float speedDown = 4.1f;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         transform.Translate(Vector3.down * speedDown * Time.deltaTime);
    19.  
    20.         if (transform.position.y < -6.1f)
    21.         {
    22.             transform.position = new Vector3(Random.Range(-13f, 13f), 7f, 0);
    23.         }
    24.  
    25.     }
    26.  
    27.     private void OnTriggerEnter2D(Collider2D other)
    28.     {
    29.         if (other.tag == "Player")
    30.         {
    31.             other.transform.GetComponent<Player>().Damage();
    32.             Destroy(this.gameObject);
    33.         }
    34.  
    35.  
    36.         if (other.tag == "Bullet")
    37.  
    38.         {
    39.  
    40.             Destroy(other.gameObject);
    41.             Destroy(this.gameObject);
    42.         }
    43.     }
    44.  
    45.  
    46. }
    47.  
     
  17. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Your original script has incorrect capitalization. You have it as "OntriggerEnter2d" where it should be "OnTriggerEnter2D".
     
  18. Deleted User

    Deleted User

    Guest

    It doesn't work with the right capitalisation either.
     
  19. Deleted User

    Deleted User

    Guest

    What's the difference between the objects themselves?
     
  20. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    So I was teaching class last night and had them do this project explaining there was a bug in the script, and when I helped one of the students I was shocked to see to it work for them and ti worked for all 20 students? Maybe it was the caps of just a error with the VS script I created?

    This worked

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Powerup : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     float speed = 3.041f;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         transform.Translate(Vector3.down * speed * Time.deltaTime);
    20.  
    21.         if (transform.position.y < -4.6f)
    22.         {
    23.  
    24.             Destroy(this.gameObject);
    25.         }
    26.     }
    27.  
    28.     private void OnTriggerEnter2D(Collider2D other)
    29.     {
    30.         if (other.tag == "Player")
    31.         {
    32.  
    33.             Destroy(this.gameObject);
    34.         }
    35.     }
    36. }
    37.  
     
  21. Deleted User

    Deleted User

    Guest

    It's the same script.
    • this. is useless you need to remove it,
    • other.tag == is not recommended; other.gameObject.CompareTag("Player")) is recommended instead.

    Your first script:
    Your last code:
     
  22. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    168
    Thanks for the tips. I will make changes

    But the script IS NOW working for 164 students,. I have no idea why it did not before?
     
  23. Deleted User

    Deleted User

    Guest

    What version of Unity are they using? And are the objects exactly the same as in your first post?
     
  24. ManieBesselaar

    ManieBesselaar

    Joined:
    Nov 1, 2018
    Posts:
    30
    Hi there. I have been where you are now. Cannot quite remember how I solved it, but I think I may be able to help.

    May I ask.
    On your power up and player, are both set to trigger?

    If you log here
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3. Debug.Log("Triggered by " + other.tag);
    4.         if (other.tag == "Player")
    5.         {
    6.             Destroy(this.gameObject);
    7.         }
    8.     }
    Does it show up in the log. ( You mentioned that the trigger does not fire, just want to be sure.

    I will look at my old space shooter code in the mean time. Had the same problem, think I made a note in there somewhere.
     
  25. Deleted User

    Deleted User

    Guest

    It doesn't for me with this piece of code:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         Debug.Log("Triggered by " + gameObject);
    4.  
    5.         if(other.gameObject.CompareTag("Player"))
    6.         {
    7.            
    8.             Destroy(other.gameObject);
    9.         }
    10.     }
    gameObject being the object that carries the script and whether the box colliders are both set "trigger" or only the player's. other.gameObject is the player that must be destroyed after gameObject hits it.
     
  26. ManieBesselaar

    ManieBesselaar

    Joined:
    Nov 1, 2018
    Posts:
    30
    Tried this out in unity 2019.3.0.f1

    Played around a bit and got it. @bobisgod234 had it right.

    I duplicated your setup and copied your code.
    Then I commented out your OntriggerEnter2d function and recreated it.
    problem solved.

    On OnTriggerEnter2D (capt T cap D ) is the correct spelling for the broadcast event.
    The editor wont complain if you mis spell it, but the function will technically not have the same name as the broadcast and will therefore not be triggered.

    If your try this:
    Code (CSharp):
    1. private void OntriggerEnter2d(Collider2D other)
    2.     {
    3.         Debug.Log("Powerup script triggered function 1");
    4.         if (other.tag == "Player")
    5.  
    6.         {
    7.             Destroy(this.gameObject);
    8.       }
    9.  
    10.     }
    11.  
    12.     private void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         Debug.Log("Powerup script triggered function 2");
    15.         if (other.tag == "Player")
    16.  
    17.         {
    18.             Destroy(this.gameObject);
    19.         }
    20.     }
    in the editor you will notice 3 things.

    1. Only one of the functions will trigger when the colliders touch.
    2. The editor will not complain about 2 functions with the same name.
    3. OnTriggerEnter2D will be blue (I assume to indicate that it is the a broadcast receiver) while OntriggerEnter2d
    will be white text like any function you define .

    So if you just capitalise your T and D you should be good to go.

    Hope this helps.
     
  27. Deleted User

    Deleted User

    Guest

    Well, well, the problem is with Unity 2020.1; the scripts do not work in that version. I might have an idea about that but I'll check later.

    For now, I have reproduced the OnTriggerEnter2D demo in the documentation here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

    The demo works in 2019.2.19f1; it doesn't work in 2020.1.0a20.

    Then, in both versions I have modified Example2 so that it destroys Example1 on trigger enter and it works in 2019.2.19f1; it doesn't work in 2020.1.0a20.

    Edit: I recreated the scripts in an brand new project with 2020.1.0a20, and now, everything works smoothly. Go figure...
     
    Last edited by a moderator: Jan 28, 2020
  28. ManieBesselaar

    ManieBesselaar

    Joined:
    Nov 1, 2018
    Posts:
    30
    Glad you got this sorted.
    I just re read this thread and realized that I completely missed the part where you DID FIX THE CAPITALIZATION.
    I actually copied and pasted your first code to recreate the problem, I should have checked the later code blocks more thoroughly.

    My bad. Sorry about that. It was rather late at night over here.

    I will still leave my post up though, because I think that this is a mistake that could catch out some people (like me) and is easy to miss, so maybe some future traveler will find this useful.

    This is kind of weird , but all is well that ends well I guess.
     
  29. Deleted User

    Deleted User

    Guest

    My code has always been correctly written; it's not me who created this thread. :)
     
  30. ManieBesselaar

    ManieBesselaar

    Joined:
    Nov 1, 2018
    Posts:
    30
    Sorry. I have trouble following sometimes.

    Any how. I hope some good came out of my bumbling around.
     
  31. Deleted User

    Deleted User

    Guest

    No problem. :)