- Home /
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!
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).
I see, can you explain how to use $$anonymous$$onoBehaviour OnDestroy() ? I am not so familiar w it.
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.
Your answer
Follow this Question
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