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

Destroy GameObject not working

Discussion in 'Scripting' started by louisEmery, Oct 17, 2022.

  1. louisEmery

    louisEmery

    Joined:
    May 10, 2021
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class BulletScript : MonoBehaviour
    {
    private Vector3 mousePos;
    private Camera mainCam;
    private Rigidbody2D rb;
    public float force;
    // Start is called before the first frame update
    void Start()
    {
    mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
    rb = GetComponent<Rigidbody2D>();
    mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
    Vector3 direction = mousePos - transform.position;
    Vector3 rotation = transform.position - mousePos;
    rb.velocity = new Vector2(direction.x, direction.y).normalized * force;
    float rot = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0, 0, rot + 90);
    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnTriggerEnter2D(Collider2D collision)
    {
    Debug.Log("hit");
    void DestroyGameObject()
    {
    Destroy(gameObject);
    }
    }

    }

    it says I never use DestroyGameObject
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    So.. Hard.. To... Read. My poor old eyes. Please use code Tags.

    Right out the gate, i see something that looks pretty ugly i think though... One does not Destroy a GameObject with 'void Destroy()'...

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D collision)
    2. {
    3. Debug.Log("hit");
    4. void DestroyGameObject()
    5. {
    6. Destroy(gameObject);
    7. }
    8. }

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D collision)
    2. {
    3.      Debug.Log("hit");
    4.      Destroy(gameObject);
    5. }
    Try something like that instead.
     
  3. louisEmery

    louisEmery

    Joined:
    May 10, 2021
    Posts:
    2
    Thanks a lot, it worked. I'm relatively new to coding. btw what exactly are code tags? ty again.
     
    Homicide likes this.
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    when you are posting, if you look at the optins on the header of the 'Text Area' you are posting in, there is one with 'CODE' beside it. It will allow you to put all your code in there and save us all some migraines :D
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Welcome to the forums.

    Please note, you should use the Scripting forum for general scripting questions. Only use the 2D forum when asking something about 2D and not simply when you're creating a 2D game and have any issue.

    Here's the link to using code-tags. Note that you can edit your post to include this too.

    I'll move your post to the Scripting forum.

    Thanks.