- Home /
Unable to Destroy individual object from prefab object
So I have been pouring through forums and answers as well as google and have been unable to find a solution to my specific issue even after trying some of the answers to similar questions but none of those have seemed to be attempting the exact same goal as mine. Hoping I can get some help on this as I am still new to some of the ins and outs for coding
Here is the goal: I am trying to adjust the Breakout style game tutorial to allow me to instantiate an object with multiple prefab blocks as children. I need to break each child individually in order to score points. The parent prefab is just to make it simpler to instantiate the object a single piece of geometry with multiple parts to destroy.
The issue: I am unable to destroy the individual blocks independent of the prefab I have tried adjusting my code a few different ways to try and achieve this however I am unable to get even the main prefab to break. if I place a single brick into the scene I am able to break it and have it count in scoring so I think there is some barrier I have been unable to find that is preventing the individual blocks from breaking off of the prefab.
I have tried using tags and creating a script on the parent object for the prefab to reference the child. But still no luck please see the code I have used below. It may also be worth mentioning that I am using this in a 2.5d style game with the geometry rotating on the y axis.
Ball.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public float ballInitialVelocity=600f;
private Rigidbody rb;
private bool ballInPlay;
void Awake ()
{
rb = GetComponent<Rigidbody> ();
}
void Update ()
{
if (Input.GetButtonDown ("Fire1") && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce (new Vector3 (ballInitialVelocity, ballInitialVelocity, 0f));
}
}
}
Bricks.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public float ballInitialVelocity=600f;
private Rigidbody rb;
private bool ballInPlay;
void Awake ()
{
rb = GetComponent<Rigidbody> ();
}
void Update ()
{
if (Input.GetButtonDown ("Fire1") && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce (new Vector3 (ballInitialVelocity, ballInitialVelocity, 0f));
}
}
}
DestroyChild.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyChildren : MonoBehaviour {
void GetRidOfChild()
{
GM.instance.ChildDestroy ();
}
}
GM.CS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GM : MonoBehaviour {
public int lives = 3;
public int bricks= 35;
public float resetDelay=1f;
public Text livesText;
public Text infoText;
public GameObject gameOver;
public GameObject youWon;
public GameObject bricksPrefab;
public GameObject paddle;
public GameObject deathParticles;
public static GM instance = null;
private GameObject clonePaddle;
private int score = 0;
private Scene currentScene;
// Use this for initialization
void Awake ()
{
//Get a reference to the current scene
Scene currentScene = SceneManager.GetActiveScene();
if (instance == null)
instance = this;
else if (instance != this)
Destroy (gameObject);
Setup ();
}
public void Setup ()
{
clonePaddle = Instantiate (paddle, transform.position, Quaternion.identity) as GameObject;
Instantiate (bricksPrefab, transform.position, Quaternion.identity);
}
void CheckGameOver ()
{
if (bricks < 1)
{
youWon.SetActive (true);
Time.timeScale = .25f;
Invoke ("Reset", resetDelay);
}
if (lives < 1)
{
gameOver.SetActive (true);
Time.timeScale = .25f;
Invoke ("Reset", resetDelay);
}
}
void Reset()
{
Time.timeScale = 1f;
//Tell the SceneManager to load the current scene (which reloads it)
SceneManager.LoadScene(currentScene.buildIndex);
}
public void LoseLife()
{
lives--;
livesText.text = "Lives" + lives;
Instantiate (deathParticles, clonePaddle.transform.position, Quaternion.identity);
Destroy (clonePaddle);
Invoke ("SetupPaddle", resetDelay);
CheckGameOver ();
}
void SetupPaddle()
{
clonePaddle = Instantiate (paddle, transform.position, Quaternion.identity) as GameObject;
}
public void DestroyBrick ()
{
bricks--;
CheckGameOver ();
}
//Called from the EnemyHealth script when an enemy is defeated
public void AddScore(int points)
{
//Add points to the player's score
score += points;
//If the info text UI element exists, update it to say the player's score
if (infoText != null)
infoText.text = "Score: " + score;
}
public void ChildDestroy ()
{
Destroy (gameObject);
}
}
I appreciate any help your a willing to give hopefully it is just something simple I have missed that I am overlooking.
Thank you in advance for your help.
You didn't copy in Brick.cs (it's ball), I think we need to see that as nothing is calling GetRidOfChild.
So here is the appropriate script I am using for
Bricks.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bricks : $$anonymous$$onoBehaviour {
public GameObject brickParticle;
void OnCollisionEnter (Collision other)
{
Instantiate(brickParticle, transform.position, Quaternion.identity);
G$$anonymous$$.instance.DestroyBrick();
Destroy (gameObject);
}
}
I believe here:
public void ChildDestroy ()
{
Destroy (gameObject);
}
you are destroying the gameObject that holds your Game$$anonymous$$anager. Try this ins$$anonymous$$d:
public void ChildDestroy (GameObject childToDestroy)
{
Destroy (childToDestroy);
}
Then you can call this from the scripts you test the collisions.
thank you for the responses I will give these items a shot and update with results Cheers!
so I apologize for taking so long to get back on results but I have tried the above suggestion and still have been unable to make this work...if you have any others I would be most welcome.
The search continues.
Your answer
![](https://koobas.hobune.stream/wayback/20220612105612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create destroyed prefab 1 Answer
How to destroy instantiated objects. 1 Answer
Minor bugs in my gun shooting script 1 Answer
My prefab isn't getting destroyed 1 Answer
Destroying a instantiated prefab? 2 Answers