Search Unity

destroying just one of the prefabs

Discussion in 'Scripting' started by Amysuzanv, Feb 12, 2021.

  1. Amysuzanv

    Amysuzanv

    Joined:
    Feb 12, 2021
    Posts:
    3
    I have made a prefab with three object in it, but while I want my player to be able to destroy one of the objects (the object it collides with) all three get destroyed! Does anyone know how to fix this?


    my code:

    public class brick : MonoBehaviour
    {
    public int hits = 1;
    public int points = 100;

    void Start()
    {

    }


    void Update()
    {

    }

    private void OnCollisionEnter(Collision collision)
    {
    hits--;
    //score points
    if (hits <= 0)
    {
    Destroy(gameObject);
    }
    }

    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What object is this script attached to?
     
  3. Amysuzanv

    Amysuzanv

    Joined:
    Feb 12, 2021
    Posts:
    3
    It's attached to "brick"
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Let me be more clear. You said you have a prefab with 3 objects (none of which you've identified as the "brick"). Is there 1 instance of this script attached to the parent, is there 1 instance of the script attached to each of the 3 child objects? Where are your colliders? Is there a rigidbody? When the objects get destroyed, is it the individual child objects, or is it really the parent object getting destroyed?

    Be forthcoming with information. You state this script is attached to the brick, but where in your original post did you even mention which object is a brick? We aren't mind readers.
     
    PraetorBlue likes this.
  5. Amysuzanv

    Amysuzanv

    Joined:
    Feb 12, 2021
    Posts:
    3
    I'm sorry, as I wrote the last reply I already knew that what I wrote was very unclear! I'm a total newbie with programming and I've actually been following a tutorial on this, so I'm very bad at explaining things yet. I have made a prefab of "brick which is a cube" in which are the original one and two copies. With the code I have in my original post my "player" (a ball) is supposed to collide with one of the bricks and then only that one should be destroyed, but when the ball collides with the brick all three of them get destroyed.

    I understand if you aren't able to help me with just the information I stated above, but this is all that I personally know unfortunately!