Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 matrix1395 · Aug 04, 2021 at 08:18 PM · objectscripting beginnerdestroy object

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?

alt text

2021-08-04-22h07-16.png (15.9 kB)
Comment
Add comment · Show 1
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 rage_co · Aug 05, 2021 at 04:17 AM 0
Share

Rather than checking if an object has been destroyed, i recommend calling the event when you are destroying the object

2 Replies

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

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

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 matrix1395 · Aug 05, 2021 at 05:25 PM 0
Share

Thank you @Hellium. This is exactly what I wanted to achieve here.

avatar image
0

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.

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 rage_co · Aug 05, 2021 at 10:59 AM 0
Share

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

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

148 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

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

Reversing height fog 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