Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Valincurr · Jan 25, 2017 at 10:21 PM · c#prefabdestroy

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image juicyz · Jan 26, 2017 at 12:07 AM 0
Share

You didn't copy in Brick.cs (it's ball), I think we need to see that as nothing is calling GetRidOfChild.

avatar image Valincurr juicyz · Jan 26, 2017 at 01:15 PM 0
Share

Cannot believe I did not see that .

avatar image Valincurr Valincurr · Jan 26, 2017 at 07:00 PM 0
Share

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);
 
     }
 
 }
 
avatar image UDM1983 · Jan 26, 2017 at 07:45 AM 0
Share

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.

avatar image Valincurr · Jan 26, 2017 at 12:08 PM 0
Share

thank you for the responses I will give these items a shot and update with results Cheers!

avatar image Valincurr Valincurr · Jan 29, 2017 at 08:26 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges