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. Dismiss Notice

Question (new to unity) i have sound code for deleting game objects but it says there is compiling errors

Discussion in 'Scripting' started by maiothegreatest, Jun 14, 2023.

  1. maiothegreatest

    maiothegreatest

    Joined:
    Jun 14, 2023
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    namespace collisiontrigger
    {
    public class collisiontrigger : MonoBehaviour
    {
    // Start is called before the first frame update
    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("enemy bean"))
    {
    Destroy(gameObject);
    }
    }
    }
    }

    this is my code for deleting game objects with this tag on collision and when i try to put the script onto my bullet prefab it says there are compiling errors but in visual studio it says there isn't and i dont even know if the code will work and i really want to test it but can't does anybody know a better way to do this?
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Please use code tags for the code. If you click on any other thread with some code in it, you will see that it renders properly, while yours does not.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace collisiontrigger
    6. {
    7.   public class collisiontrigger : MonoBehaviour
    8.   {
    9.     // Start is called before the first frame update
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.       if (other.gameObject.CompareTag("enemy bean"))
    13.       {
    14.         Destroy(gameObject);
    15.       }
    16.     }
    17.   }
    18. }
    I initially wrongly assumed you had one brace too many, but that's not the case.
    What is the message of the error you're getting? It could be that your class name cannot be the same as the namespace, but I can't tell from the top of my head.

    If you're a beginner, you don't really need namespaces.
    Do this instead.
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CollisionTrigger : MonoBehaviour
    6. {
    7.   // Start is called before the first frame update
    8.   void OnTriggerEnter(Collider other)
    9.   {
    10.     if (other.gameObject.CompareTag("enemy bean"))
    11.     {
    12.       Destroy(gameObject);
    13.     }
    14.   }
    15. }
     
    maiothegreatest likes this.
  3. maiothegreatest

    maiothegreatest

    Joined:
    Jun 14, 2023
    Posts:
    4
    thank you for showing me how to put the code in the thing but the the destroy code still seems to be not working it might be something i didn't set up correctly ill do some more digging online to see if i can find anything.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Well I didn't even attempt to fix it, just showed you that there might be a name collision.

    Your next problem is that
    gameObject
    is unknown in that context. You probably wanted to type
    other.gameObject
    . You already do it two lines above it. Why do you think it should be any different here?

    Edit:
    Nah, it's not that.
    gameObject
    is known locally, it's ok. Can you tell me the message of the error (if there is any)?
    This should work. Try creating a new scene, and running this in isolation. Maybe there are some weird interactions between the other objects you have.

    Edit2:
    Oh, no, wait. Do this
    Code (csharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.   if (other.gameObject.CompareTag("enemy bean"))
    4.   {
    5.     Debug.Log($"triggered by {other.gameObject.name}");
    6.     Destroy(gameObject);
    7.   }
    8. }
    This will write a message to your console. If this doesn't do anything, then your tag is wrongly typed or you haven't set the tags properly.
     
    Last edited: Jun 14, 2023
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Updated my answer.
     
  6. maiothegreatest

    maiothegreatest

    Joined:
    Jun 14, 2023
    Posts:
    4
    interesting i tried using the "enemy bean" and it didnt work but when i wrote "enemy" and then changed the tag on the bullet to enemy and it did work im not sure why but thank you very much when i posted this thread i wasn't sure if anyone would even respond but you did almost immediately and now my code works perfectly and it deletes the object when the bullet collides with it.

    edit: your code
     
    Nad_B and orionsyndrome like this.
  7. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
    And that's why using Tags (or any other string) to identify objects is a bad idea, as they're error prone and hard to debug if you misspell a single character, casing or add an unwanted space at the beginning/end (which happens more often than you may think). There are better alternatives like Interfaces for eg.
     
  8. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
    And that's why using Tags (or any other string) to identify objects is a bad idea, as they're error prone and hard to debug if you misspell a single character, casing or add an unwanted space at the beginning/end (which happens more often than you may think). There are better alternatives like Interfaces for eg.

    Code (CSharp):
    1. // IEnemy interface, all enemies implement it
    2. public interface IEnemy { }
    3.  
    4. public class Bean : MonoBehavior, IEnemy
    5. {
    6.     // your Bean enemy behavior, attach this to your Bean game object in the Editor
    7. }
    And now your collision code becomes:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.gameObject.TryGetComponent<IEnemy>(out var enemy))
    4.     {
    5.         Debug.Log($"triggered by Enemy {enemy}");
    6.         Destroy(gameObject);
    7.     }
    8. }
    The cool thing is an interface can declare methods and properties inside it, that all classes implementing it MUST declare, for eg you can have something like this:

    Code (CSharp):
    1. // IEnemy interface, all enemies implement it
    2. public interface IEnemy
    3. {
    4.     void HadCollision();
    5. }
    6.  
    7. public class Bean : MonoBehavior, IEnemy
    8. {
    9.     // your Bean enemy behavior, attach this to your Bean game object in the Editor
    10.     // it MUST implements void HadCollision() of IEnemy
    11.  
    12.     public void HadCollision()
    13.     {
    14.         // Do something, maybe play a sound, decrease Bean health or do some fancy particle effects...
    15.     }
    16. }

    And now you can call the "HadCollision" method:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.gameObject.TryGetComponent<IEnemy>(out var enemy))
    4.     {
    5.         Debug.Log($"triggered by Enemy {enemy}");
    6.         enemy.HadCollision(); // we know that all IEnemies have this method!
    7.         Destroy(gameObject);
    8.     }
    9. }
    And like this, you can have different enemy types with different HadCollision implementation without changing any collision code.
     
    Last edited: Jun 15, 2023
    Ryiah and orionsyndrome like this.