- Home /
Detect if object was destroyed and if yes activate another one
Hello Unity folks. I am trying to create functionallity which will show me one text object only if the another one is detected as a destroyed.
Here you can find what I already tried to do DestroyedChecker class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyedChecker : MonoBehaviour
{
public GameObject gameTextObject;
public GameObject gameTextObject2;
void Update()
{
if(gameTextObject == null)
{
gameTextObject2.SetActive(true);
}
}
}
And TextTimer class which removes the object after 5 seconds:
using UnityEngine;
using System.Collections;
public class TextTimer : MonoBehaviour
{
public float time = 5;
public void DestroyText()
{
Destroy(gameObject, time);
}
}
First text object disappears after 5 secund as expected but the DestroyedChecker not working here properly and I am not sure why exactly. Any tips?
Rather than checking if an object has been destroyed, i recommend calling the event when you are destroying the object
Answer by Hellium · Aug 05, 2021 at 11:05 AM
1. Get rid of your DestroyedChecker
2. Replace your TextTimer
class by this one
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
public class TextTimer : MonoBehaviour
{
public float time = 5;
public UnityEvent Destroyed;
public void DestroyText()
{
Destroy(gameObject, time);
}
private void OnDestroy()
{
if (Destroyed != null)
Destroyed.Invoke();
}
}
3. A block similar to the OnClick event of the button should appear in the inspector. Drag & drop the gameObject to activate and select GameObject > SetActive
and check the chekbox
Thank you @Hellium. This is exactly what I wanted to achieve here.
Answer by AcidmanRPGz · Aug 05, 2021 at 10:45 AM
I haven’t tested this, but try this out:
if (GameTextObject = null)
{
GameTextObject2.SetActive(true);
}
Or, if you aren’t actually destroying the object, but simply disabling it:
if (GameTextObject.activeSelf)
{
GameTextObject2.SetActive(true);
}
Tell me if these work when you put them in the update or fixedupdate functions.
Op is already going with the first approach qnd it isn't working, plus you missed a = mark and for the second method, im pretty sure it will return a null reference exception error since the object has been destroyed
Your answer

Follow this Question
Related Questions
How to destroy an object gradually? 1 Answer
What are GameTiles? 1 Answer
Help with a simple procedural generation 0 Answers
Unity's Breakout Tutorial Ball Issue. 1 Answer
swapping out an object on collision 2 Answers