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

Sorry for posting this again, but objects aren't destroying.

Discussion in 'Scripting' started by FurryMaster15, Jul 16, 2023.

Thread Status:
Not open for further replies.
  1. FurryMaster15

    FurryMaster15

    Joined:
    May 28, 2023
    Posts:
    11
    The script for my projectile in the project is;

    public class ProjectileScript : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {

    }

    public float speed = 100;
    void Update()
    {
    transform.Translate(0, 1 * Time.deltaTime * speed, 0);
    }
    void OnTriggerEnter (Collider other) {
    Destroy(gameObject);
    Destroy(other.gameObject);
    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please read the following which shows you to how to help yourself with these kinds of simple problems by debugging. If you've already debugged it then posting your results is the bare minimum you can do if you want help.

    Also, please edit your post to use code-tags; don't post as plain text please.

    Finally, note that a code snippet likely doesn't provide enough information to say what is happening. All you've said is that "objects aren't destroying". No idea what that means. No idea if they're even coming into contact and calling that trigger function. Debugging will verify that and you can do it before making a post too.

    Hope it helps.
     
    DragonCoder and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Does your Projectile object actually has a Rigidbody component attached? If not you don't get any physics interactions from it. Moving objects with a collider always needs to have a Rigidbody component.
     
  4. FurryMaster15

    FurryMaster15

    Joined:
    May 28, 2023
    Posts:
    11
    I just tried that, but it didn't work.
     
  5. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    You should switch your destroy commands. By destroying the game object that holds the script first, most likely makes that the Destroy(other) never executes.

    You can easily debug this by replacing the Destroy(other) with for example Test();
    then write a Test function that will write some text to the Console.
     
  6. FurryMaster15

    FurryMaster15

    Joined:
    May 28, 2023
    Posts:
    11
    Actually, I saw in a discord server it was because it was a 2d project.
     
Thread Status:
Not open for further replies.