Copy of GameObject gets destroyed instead of GameObject itself

Hey guys.
I have a little problem which i cant really get behind even though its probably not a hard one. I have two walls (one is a copy) and if i damage the original one, the damage gets inflicted onto the copy and then ,after it recieves enough damage, the copy gets destroyed, even though i tried to damage the original.
Heres a video of that happening: The Video

Anyway I have a “ObjectHealth” Script which is basically a very simple and normal health script with a certain life value which can get reduced and if reached zero the gameObject gets destroyed.
Heres the Code:

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

public class ObjectHealth : MonoBehaviour
{
    public float maxHealth = 200f;
    public float currentHealth;
    public GameObject deathEffectOne;
    public bool isDead = false;
    public GameObject objectLOL;

    void Start()
    {
        currentHealth = maxHealth;
        Debug.Log(currentHealth);
    }

    public void TakeDamage(float amount)
    {
        currentHealth = currentHealth - amount;

        if (currentHealth <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        DeathEffect(deathEffectOne);
        Destroy(objectLOL);
        isDead = true;
    }

    public void DeathEffect(GameObject deathEffect)
    {
        Instantiate(deathEffect, transform.position, transform.rotation);
    }
}

Would be nice if anyone could help :slight_smile:

[Updated Answer]
Hey there,

You should get the reference to the Wall ObjectHealth script from the Object you collided with:

     private void OnTriggerEnter2D(Collider2D collision)
          {
      
              if (collision.tag == "Wall")
              {
                  Instantiate(wallImpactEffect, transform.position, transform.rotation);
                  BulletExplosion(impactEffectOne);
     ObjectHealth otherObjectHealth = collision.gameObject.GetComponent<ObjectHealth>();
                  otherObjectHealth.TakeDamage(damage);
                  Destroy(gameObject); //Thats for the bullet itself
              }
      
      
          }

This following part is problematic because you have multiple objects with tag “wall” and call a function which only returns you one instance:

    private ObjectHealth oH;
     
         void Start()
         {
             oH = GameObject.FindGameObjectWithTag("Wall").GetComponent<ObjectHealth>();
     
         }

Here is another answer that explains how to get a script from the gameobject you collided with:
https://answers.unity.com/questions/948013/get-a-game-objects-reference-from-collision.html

Hard to say. I’d say that most likely the problem lies on how you create the copy.

It might be that it is a reference and hence goes back to the original object instead.

Are you familiar with the concept of reference vs value in C#? That’s what I would look into. I’m not in unity right now but ai believe you can probably use « .clone » to create a new value of your wall.