Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 27, 2020 at 01:36 AM · gameobjecteventlistener

Game Object not self destroying after Destroy(gameObject)

Here's a video that shows what's happening: Click Here (YouTube)

I don't know why this is happening. I suspect it might have something to do with using a UnityEvent that Invokes a function within a component attached to the same Game Object as the script that calls that event, but I have no clue on how that works or how I should fix it. I tried disabling the HealthManager script before destroying the Game Object; no luck. Tried calling Destroy(gameObject) every frame if health gets to 0; no luck, got a "GameObject you are trying to access has been destroyed" error. And why does my code work almost every time, but it just randomly fails (that's what really bugs me out)? Does anyone have an idea of how to fix this? I got this in the middle of a game jam, so it's really stressful :/

Here are the pieces of code highlighted in the video:
(HealthManager.cs)

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Security.Cryptography;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.SceneManagement;
 
 public class HealthManager : MonoBehaviour
 {
     public float maxHealthPoints;
     public float healthPoints;
     public float defense = 1;
     public GameObject hitParticles;
     public GameObject deathParticles;
     public bool isDead;
     public HealthBar healthBar;
 
     public UnityEvent onDeath;
 
     private void Start()
     {
         if (maxHealthPoints == 0)
             maxHealthPoints = healthPoints;
 
         if (healthBar)
         {
             healthBar.SetMaxHealth(maxHealthPoints);
             healthBar.SetHealth(healthPoints);
         }
     }
 
     public void ReceiveDamage(float amount)
     {
         healthPoints -= amount / defense;
         if (healthBar)
             healthBar.SetHealth(healthPoints);
         if (healthPoints <= 0)
         {
             Destroy(Instantiate(deathParticles, transform.position, Quaternion.identity), 1);
             healthPoints = 0;
             isDead = true;
             onDeath.Invoke();
         }
         else
             Destroy(Instantiate(hitParticles, transform), 1);
     }
 
     public void ReceiveHealth(float amount)
     {
         healthPoints += amount;
         if (healthPoints > maxHealthPoints)
             healthPoints = maxHealthPoints;
         if (healthBar)
             healthBar.SetHealth(healthPoints);
     }
 }

(E2.cs - Enemy Behaviour)

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using Pathfinding;
 using Pathfinding.Util;
 using UnityEngine;
 using UnityEngine.Events;
 
 // Enemy 2: Runs towards player with a knife. Low Health, Mid Damage, Mid Speed, Low Defense, Mid Range.
 
 public class E2 : MonoBehaviour
 {
     public int scorePoints;
     
     public string targetTag;
     public float angleOffset;
     
     private Item knifeItem;
     private Knife knife;
     private AIPath _aiPath;
     private AIDestinationSetter _aiDestination;
 
     private GameObject target;
 
     private void Start()
     {
         _aiPath = GetComponent<AIPath>();
         _aiDestination = GetComponent<AIDestinationSetter>();
         
         knifeItem = GetComponentInChildren<Item>();
         knife = GetComponentInChildren<Knife>();
     }
 
     void Update()
     {
         target = GameObject.FindWithTag(targetTag);
         if (target)
         {
             if (Vector3.Distance(transform.position, target.transform.position) <=
                 transform.localScale.y / 2 + knife.range)
             {
                 _aiPath.enabled = false;
 
                 Vector3 dir = target.transform.position - transform.position;
                 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + angleOffset;
                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 
                 knife.attack = true;
             }
             else
             {
                 _aiPath.enabled = true;
                 _aiDestination.target = target.transform; // For when the target changes.
                 knife.attack = false;
             }
         }
     }
 
     public void OnDeath()
     {
         PlayerPrefs.SetInt("lastScore", PlayerPrefs.GetInt("lastScore") + scorePoints);
         
         knife.attack = false;
         knife.canAttack = true;
         Item iog = Instantiate(knifeItem.selfPrefab, transform.position, transform.rotation).GetComponent<Item>();
         iog.UpdatePickupbleTrigger(true);
         iog.UpdateActionCollider(false);
         iog.UpdateMainBehaviour(false);
 
         Destroy(gameObject);
     }
 }

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

3 Replies

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

Answer by UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 28, 2020 at 05:26 PM

Ok, I figured it out. I'm really sorry if you're having this problem and are reading this thread, because in my case it was just due to my stupidity :/ Every item on my game has a "isPickedUp" bool. When that bool stays false for too long, the item self destructs, so that the items that were dropped on the ground clean themselves up. When the player picks an item up (or throws it away) the variable is updated. I, however, forgot to do so with the enemies. So every enemy would lose their item if they survived long enough. I would like to thank and apologize @ADiSiN and @Dev1910 for their time.

Comment
Add comment · 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
0

Answer by ADiSiN · May 27, 2020 at 02:28 AM

Hi!

I couldn't find the issue within your script, however I would like to point out that moment in your video:alt text So, what happens is that you are killing other type of enemies and at the same time your future undestroyable enemy loses its sword.


It can be coincidence, but as we can see here:alt text Your death animation is still playing (the explosive one), but it fails to Destroy even though animation played twice (2-nd and 4-th).

Therefore I assume next thing: for some reason sometimes by destroying one of your enemies your another enemy loses(?) its sword, so when you Invoke Death it gets called, but it throws an error here:

  Item iog = Instantiate(knifeItem.selfPrefab, transform.position, transform.rotation).GetComponent<Item>();

Where it should create prefab of your sword therefore the Destroying never reaches since code stops.


You can test it out by commenting out for a time the whole iog section in OnDeath() and I assume you will not encounter the issue.


I hope it will help you.


timeline-2.png (231.0 kB)
timeline-1.png (140.7 kB)
Comment
Add comment · Show 4 · 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 UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 27, 2020 at 12:58 PM 0
Share

Thank you so much for your detailed answer, it really helped me. I'll be away from my computer today, but once I'm back I'll try what you suggested.

Thanks, once again.

avatar image UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 28, 2020 at 01:59 PM 0
Share

Hey! I commented all the item dropping section of the code out. It definitely reduced how often those undead enemies would show up. But, for some reason, they still are, but they're not invincible anymore, I'm now able to kill them. Weird. I'll have to leave it as it is, otherwise I won't have time to do everything I have to. But thank you for you help!

avatar image ADiSiN UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 28, 2020 at 02:06 PM 0
Share

Hi!

Since you have limited time I would suggest then to make your code more safe in terms of execution, for example, when you are instantiating iog, you can simply do this:

 if(knifeItem != null)

And only in that case create prefab. Similar to all other stuff related to the refrences, so in case if something went missing it will not crash leaving the rest of the code unexecuted, but simply will not execute certain part of the code and move on.

avatar image UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b ADiSiN · May 28, 2020 at 04:21 PM 0
Share

Good Idea. I thought about writing something along the lines of if (!knifeItem) Destroy(gameObject); but that seems better. Thanks :)

avatar image
0

Answer by DevGa_me · May 27, 2020 at 05:23 AM

You can try instantiating particles not with transform but with transform.position with quaternion.identity.

It can be because when a gameobject is destroyed, info about transform is lost, but its position is still there untill all of the behaviour attached to that object is executed.

@UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b

Comment
Add comment · Show 4 · 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 UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 27, 2020 at 12:56 PM 0
Share

Hm, I don't know if I posted the code here, but the way I handle particles is by "Destroy(Instantiate(particles, pos, rot), 1);". So the particles are destroyed after a second. And one of the first solutions I tried was to create a "ForceDestroy" coroutine as such: IEnumerator ForceDestroy() { while (gameObject) { Destroy(gameObject); yield return null; } } Even then, it didn't destroy the object :/

avatar image DevGa_me · May 27, 2020 at 02:36 PM 0
Share

You have instantiated particles using transform, instantiate them using trasform.position with quaternion.identity. see line 45 in the first script.

@UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b

avatar image UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b DevGa_me · May 28, 2020 at 01:21 PM 0
Share

Well, I need it to be transform. transform, in that case, means that the particles will be instantiated as a child of that gameObject, an thus that the particles will have a local position relative to that gameObject. I need that, otherwise the particles will be left behind as the object moves.

avatar image DevGa_me UDN_7bcbe08e-783e-45c6-a9b2-4e942df1984b · May 28, 2020 at 02:24 PM 0
Share

You can instantiate particle and then parent them

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

206 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

Related Questions

Any way to pass the function of a listener as a string? 1 Answer

How do i declare an event listener? 2 Answers

Fire an event when gameObject has been renamed? 2 Answers

How to change a Player direction by colliding a Game Object 1 Answer

UnityEvent prevent listener from being added twice 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