Search code examples
c#unity-game-enginedestroygameobject

Destoying prefab, when the player hit it


Is it possible to destroy only one clone of the prefab, (I don't know it's exact name, sorry) when the player object (Sphere) hits it?

    void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player") 
    {
        Destroy (GoldCube);
    }
}

The "goldCube" is the game object name, which has a prefab called "GoldCube". And I want to destroy only one clone of it, when the main object "Sphere" (tag name: "Player") hits it.


Solution

  • Assuming the script is on the GoldCube,

    Instead of

    Destroy(GoldCube);
    

    try using

    Destroy(gameObject);
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    If this script is on the player(which in this case, it isn't),

    Destroy(other.gameObject);
    

    Documentation for Destroy