'how can I make the game object disappear?

when I was watching on YouTube, tutorials about endless runner on part 2, the game object wouldn't disappear when it hits the player

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Obstacle : MonoBehaviour
{
    public int damage = 1;
    public float speed;

    private void Update()
    {
        transform.Translate(Vector2.left * speed * Time.deltaTime);
    }

    void onTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("rocket"))
        {
            //rocket takes damage 1
            other.GetComponent<rocket>().health -= damage;
            Debug.Log(other.GetComponent<rocket>().health);
            Destroy(gameObject);
        }
    }
} 
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class rocket : MonoBehaviour
{
    private Vector2 targetPos;
    public float Yincrement;

    public float speed;
    public float maxHeight;
    public float minHeight;

    public int health = 3;

    void Update()
    {
        if (health <= 0)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

        transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.y < maxHeight)
        {
            targetPos = new Vector2(transform.position.x, transform.position.y + Yincrement);
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight)
        {
            targetPos = new Vector2(transform.position.x, transform.position.y - Yincrement);
        }
    }
}

from this one https://www.youtube.com/watch?v=FVCW5189evI and I'm confused why it didn't work, can someone tell me what is wrong?



Solution 1:[1]

if you want to destroy use this, Correct Method to get the game object when collide is: other.gameObject

void onTriggerEnter2D(Collider2D other)
 {
 if (other.CompareTag("rocket"))
  {
    //rocket takes damage 1
    other.gameObject.GetComponent<rocket>().health -= damage;
    Debug.Log(other.gameObject.GetComponent<rocket>().health);
    Destory(other.gameObject);
  }
}

Solution 2:[2]

public class megaStar : MonoBehaviour{
private Rigidbody2D rb;
private Animator _ani;

public bool canAttack = true;
[SerializeField] private Attack _attackObject;

private AudioSource _as;

public checkpoint lastCheckpoint;
public bool isDead = false;

private float _timer = 1.0f;
public float attackTimer = 2.0f;

public GameObject projectile;
public float projectileSpeed = 18.0f;
public Transform projectileAttackPoint;

public float timeDelayForNextShoot = 0.1f;
private float CONST_timeDelayForNextShoot;
// Start is called before the first frame update
void Start(){
    CONST_timeDelayForNextShoot = timeDelayForNextShoot;
    rb = GetComponent<Rigidbody2D>();
    _as = GetComponent<AudioSource>();
    _ani = GetComponent<Animator>();
    _timer = attackTimer;
}

void Update(){
    if (Input.GetKey(KeyCode.W))
    {
        rb.velocity = new Vector2(0f, 10f);
    }
    else if (Input.GetKey(KeyCode.S))
    {
        rb.velocity = new Vector2(0f, -10f);
    }
    else
    {
        rb.velocity = new Vector2(0f, 0f);
    }

    timeDelayForNextShoot -= Time.deltaTime;

    if (Input.GetKeyDown(KeyCode.K) && timeDelayForNextShoot <= 0f){
        _ani.SetBool("projectileAttack", true);
        GameObject go = Instantiate(projectile, projectileAttackPoint.position, projectileAttackPoint.rotation) as GameObject;
        go.GetComponent<Rigidbody2D>().velocity = new Vector2(-projectileSpeed, 0.0f);
        Destroy(go, 2.0f);
        timeDelayForNextShoot = CONST_timeDelayForNextShoot;
        canAttack = false;
      //  new WaitForSeconds(1);
        return;
    }
}

void FixedUpdate()
{
    if (!isDead)
    {
        Update();
    }
    else{
        rb.velocity = Vector2.zero;
        RigidbodyConstraints2D newRB2D = RigidbodyConstraints2D.FreezePositionY;
        rb.constraints = newRB2D;
    }
}

private void OnCollisionEnter2D(Collision2D collision){
    if (collision.gameObject.CompareTag("Enemy"))
    {
        GetComponent<HitPoints>().TakeDamage(1);
    }
}

}

Solution 3:[3]

Use SetActive instead of Destroy Function.

void OnTriggerEnter2D(Collider2D other)
{
    if (other.CompareTag("rocket"))
    {
        //rocket takes damage 1
       
          other.gameObject.GetComponent<rocket>().health -= damage;
     
        Debug.Log(other.gameObject.GetComponent<rocket>().health);

      gameObject.SetActive(false);// it'll Hide GameObject instead of Destroying
       // gameObject.SetActive(true);// to show again
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zain ul din
Solution 2 zain ul din
Solution 3