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
1
Question by heyhujiao · Dec 15, 2015 at 09:32 PM · 2dgameobjectinstantiateinstancesdestroy-clones

How to Destroy the instances of 2 gameObjects at the same time?

I have 2 game objects that will spawn continuously throughout the gameplay, the 2 game objects are elements of a spawner game object that I created. While I can destroy the lightning, I cannot destroy the cloud. I have 2 scripts attached to the prefabs of the 2 game object, this is attached to the lightning prefab:

 using UnityEngine;
 using System.Collections;
 
 public class lightningKills : MonoBehaviour {
 
     private Collision2D c;
     public float howHeavy;
     public bool isDestroyed = false;
 
     void Start()
     {
         howHeavy = GetComponent<Rigidbody2D>().mass;
     }
 
     void OnCollisionEnter2D(Collision2D c)
     {
         switch(c.gameObject.tag)  
         
         {
             case "Player":
                 Debug.Log("YOU DEAD!");
 
                 //Kills the Player!
                 cuteCube.SharedInstance.Death();
                 Destroy(gameObject);
                 isDestroyed = true;
                 Debug.Log("cubeCube is DEAD!");
                 break;
 
             case "Background":
                 if (gameObject.name == "lightning(Clone)")
                 {
                     Destroy(gameObject);
                     isDestroyed = true;
                     Debug.Log("Destroyed!");
                 }               
                 break;
             default:
                 Destroy(gameObject);
                 if (gameObject.name == "lightning(Clone)")
                 {
                     Destroy(gameObject);
                     isDestroyed = true;
                     Debug.Log("Destroyed!");
                 }
                 break;
 
         }
     }

 }

This is attached to the cloud prefab:

 using UnityEngine;
 using System.Collections;
 
 public class DestroyIt : MonoBehaviour
 { 
     void Awake()
     {
         GameObject getLightning = GameObject.Find("lightning");
         lightningKills theLightning = getLightning.GetComponent<lightningKills>();
 
         if (theLightning.isDestroyed)
         {
             Destroy(gameObject);
             Debug.Log("Destroyed this freaking cloud");
         }
     }
 }

Basically I am trying to access the boolean isDestroyed and when it is true, it will trigger the cloud instance to be destroyed. But it doesnt work!!!! HELP!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by phil_me_up · Dec 15, 2015 at 10:51 PM

You don't really want to be checking variables on an object which might be destroyed (i.e. there is potential for theLightning to be null, meaning that checking theLightning.isDestroyed could give you a Null reference error.

If you want something destroyed immediately, you can Use DestroyImmediate, but it's not recommended for performance reasons.

What you could do is simply check if theLightning == null, then destroy the cloud object. Alternatively you could use the MonoBehaviour OnDestroy() calls from one to start the destruction of the other.

Also note that you are checking if theLightning.isDestroyed in the Awake function of the cloud object. This seems a little odd unless you're trying to destroy the object before it's ever seen or used (or I may not understand what you're trying to achieve).

Comment
Add comment · Show 3 · 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 heyhujiao · Dec 16, 2015 at 02:17 AM 0
Share

I see, can you explain how to use $$anonymous$$onoBehaviour OnDestroy() ? I am not so familiar w it.

avatar image phil_me_up heyhujiao · Dec 16, 2015 at 11:01 AM 1
Share

It's just like the Start(), Awake(), Update() and similar calls. Something like

void OnDestroy() { //code in here }

will work fine. The actual function will be called just before the object is destroyed, so you don't need to call it yourself.

avatar image heyhujiao phil_me_up · Dec 16, 2015 at 01:17 PM 0
Share

Thanks, you have been helpful!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiating a gameObject in an array is not working for me 1 Answer

How to instantiate a new GameObject (sprite) properly? 1 Answer

Is there any way i can Instantiate a gameobject into an Instantiated gameobject as a child? 1 Answer

Checking if a gameobjects active in your heirarchy. 2 Answers

Instantiate clones with error. 1 Answer


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