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 /
  • Help Room /
avatar image
0
Question by AttentionHorse · Mar 26, 2017 at 03:49 PM · c#script.destroyinspectorgameobjects

Destroy gameobjects several times in a scene. Also destroy other instances of that gameobject.

alt text So this is my ball and two blue walls.

I use

 bw = GameObject.FindWithTag("bw");

 private void Explosion (){
             if (radius.Contains (bw)) {
                 Destroy (bw);
             }
 }

 private void OnTriggerEnter(Collider col){
     radius.Add(col.gameObject);
 }

 private void OnTriggerExit (Collider col){
     radius.Remove(col.gameObject);
 }



So each time one of the walls get into the range of "col" they should be added to the list "radius" and when the ball move too far away they should be removed from the list and not therefore not destroyed when Explosion is run.

This works for BW1 but when I reach BW2 I reach BW2 i get an null error, saying that I'm trying to access a gameobject I already destroyed. I've dragged them both into the scene from a prefab I created earlier if that matters, they are both tagged with "bw". Also, if I reach BW2 first I won't get the error but neither will it destroy that wall.

So my questions would be:

  1. How can I remove BW1 from the scene without destroying the gameobject so that I can remove additional BWs later? (or destroy the gameobject as long as I can do it on several BWs)

  2. How can I remove BW2? since the code I have doesn't seem to affect it.

Thank you very much!

17555052-10212687418329231-1094340464-n.png (1.9 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by LiloE · Mar 29, 2017 at 05:13 PM

There are several conceptual/design problems with your code, as far as we can see from what you shared.

  1. This expression: GameObject.FindWithTag("bw"); will return a single object tagged "bw". But it is not guaranteed to be any specific object in any specific order.

  2. You can use GameObject.FindGameObjectsWithTag("bw"); instead, which is better, but this brings us to the next design problem - why are you looking for these objects in the first place? You already have them in your radius collection, and judging by your logic - you're only interested in those that are already in that collection. So, why not just iterate through the collection and destroy those elements instead?

  3. Destroying the objects isn't enough, you also need to remove the object from the radius container, otherwise, next time you run the explosion, the game engine will attempt to destroy those objects again.

  4. Using SetActive instead of actually destroying is indeed better for the general performance of the game. Regarding the timing you need for this function - try something like this:

         using UnityEngine;
         using System.Collections;
         
         public class ExampleClass : MonoBehaviour {
             void Hide(float timeToWait) {
              Invoke("HideMe", timeToWait);
             }
             void HideMe() {
                 gameObject.SetActive(false);
             }
         }
     
    
Comment
Add comment · Show 2 · Share
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 AttentionHorse · Mar 29, 2017 at 06:27 PM 0
Share

Thank you very much! I'm new to this, I apologize for all the obvious errors and now follow-up questions.

I was using bw to show the problem that I only manage to destroy one wall. I have walls in four different colors and that's why I thought I needed the different tags to be able to destroy the correct one. To destroy them I have an area on the screen which activates an "explosion" associated with respective color.

Can I find the correct color somehow within the radius (since sometimes it might be more than one color in the radius) and destroy it from there, without the GameObject.FindGameObjectsWithTag("color for each obstacle");? Since I don't always want to destroy them when they are in the radius, unless the correct explosion is activated it shouldn't be removed.

Regarding removing the objects from radius I thought private void OnTriggerExit (Collider col){ radius.Remove(col.gameObject);} would do that? Does this not work when I use Destroy? Will SetActive(false); fix that in that case or do I still need to find another solution to removing them?

avatar image AttentionHorse · Mar 30, 2017 at 02:04 PM 0
Share

I've changed to

 public GameObject[] bw;
 bw = GameObject.FindGameObjectsWithTag("bw");

so now I can check

 private void Explosion (){
            for(int i=0; i < bw.Length; i++){
                   if (radius.Contains (bw[i])) {
                         bw[i].setActive(false);
                   }
            }        
 }

But when I try to use the hide method I can't get it to work on bw[i] and I can only get the bw[0] particlesystem to go off. Do I need to assign all other PS in the unity inspector or is it possible to activate them completely from the script?

Thank you very much!

avatar image
-1

Answer by herDev · Mar 27, 2017 at 08:43 AM

Hi,

So I'm assuming 'col' is your ball. If it's not already on it, you could add your collision script to this gameobject and then you can get a reference to the other 'wall' object from the 2Dcollision method (otherObject is automatically assigned) and add it a list or do whatever you want to do with it:

     void OnCollisionEnter2D(Collision2D otherObject)
     {
        radius.Add(otherObject);
     }

     private void OnCollisionExit2D(Collision2D otherObject)
     {
        radius.Remove(otherObject);
     }

ps. you'll need to set up the rigidbody's correctly too (isKinematic values etc). Also rather than destroying them, use SetActive(false) so garbage collection doesnt kick in.

Hope that helps!

Comment
Add comment · Show 1 · Share
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 AttentionHorse · Mar 29, 2017 at 11:52 AM 0
Share

The code you've written is the only thing I said that works with my code. Sorry if I didn't specify, the game is in 3d.

SetActive might work but I have a particlesystem on my BWs now. With destroy I have a delay which allows me to play this PS, but with SetActive there isn't a delay parameter. How do I solve that? Or is there a better way to do this without any of the previous functions?

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

329 People are following this question.

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

Related Questions

Destroying enemy only partially works 0 Answers

[solved] Survival Shooter - 'Player' tag mysteriously filled 2 Answers

How Create a menu in script and inspector ?? 2 Answers

How do i destroy a platform after collision with a quad? 0 Answers

Problem in Destroy GameObject on OnTriggerEnter2D 0 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