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 matthrewp · Oct 26, 2016 at 07:30 PM · destroy object

Destroy(gameObject) not working

I have two scripts that randomly spawn models in an area and then respawn ones the overlap. I can't find any errors in the code, but when Destroy(gameObject); is called in the second script, it seems to do nothing. Here is the base spawning script:

 using UnityEngine;
 using System.Collections;

 public class CreateBuildings : MonoBehaviour {

     public GameObject ThreeSBWR;
     public GameObject ThreeSB;
     public GameObject TwoSBWR;
     public GameObject TwoSB;
     public GameObject OneSBWR;
     public GameObject OneSB;
     public int ThreeSBTS;
     public int TwoSBTS;
     public int OneSBTS;
     Quaternion SpawnRotation;

     void Start ()
     {
         int a;
         int b;
         int c;
         a = ThreeSBTS;
         while (a > 0)
         {
             Vector3 SpawnLocation;
             SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
             while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
             {
                 SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
             }
             SpawnRotation.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
             float rand;
             rand = Random.Range(0, 1);
             if (rand >= 0.5f)
             {
                 Instantiate(ThreeSB, SpawnLocation, SpawnRotation);
             }
             else
             {
                 Instantiate(ThreeSBWR, SpawnLocation, SpawnRotation);
             }
             a -= 1;
         }
         b = TwoSBTS;
         while (b > 0)
         {
             Vector3 SpawnLocation;
             SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
             while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
             {
                 SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
             }
             SpawnRotation.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
             float rand;
             rand = Random.Range(0, 1);
             if (rand >= 0.5f)
             {
                 Instantiate(TwoSB, SpawnLocation, SpawnRotation);
             }
             else
             {
                 Instantiate(TwoSBWR, SpawnLocation, SpawnRotation);
             }
             b -= 1;
         }
         c = OneSBTS;
         while (c > 0)
         {
             Vector3 SpawnLocation;
             SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
             while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
             {
                 SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
             }
             SpawnRotation.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
             float rand;
             rand = Random.Range(0, 1);
             if (rand >= 0.5f)
             {
                 Instantiate(OneSB, SpawnLocation, SpawnRotation);
             }
             else
             {
                 Instantiate(OneSBWR, SpawnLocation, SpawnRotation);
             }
             c -= 1;
         }
     }
     public void Spawn(GameObject ToSpawn)
     {
         print("Call Successfull");
         Vector3 SpawnLocation;
         SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
         while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
         {
             SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
         }
         Quaternion SpawnRot;
         SpawnRot = new Quaternion(0, 0, 0, 0);
         SpawnRot.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
         Instantiate(ToSpawn, SpawnLocation, SpawnRot);
         print("(Call) Spawn Complete");
     }
 }

Here is the script that checks for overlaps:

 using UnityEngine;
 using System.Collections;

 public class CollisionCheck : MonoBehaviour {

     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.name != "Player" && col.gameObject.name != "Ground" && col.gameObject.name != "Bullet(Clone)")
         {
             GameObject.Find("GameController").GetComponent<CreateBuildings>().Spawn(gameObject);
             print("Called");
             Destroy(gameObject);
         }
     }
     void OnTriggerStay(Collider col)
     {
         if (col.gameObject.name != "Player" && col.gameObject.name != "Ground" && col.gameObject.name != "Bullet(Clone)")
         {
             GameObject.Find("GameController").GetComponent<CreateBuildings>().Spawn(gameObject);
             print("Called");
             Destroy(gameObject);
         }
     }
 }

Here is a video showing what happens with different values for ThreeSBTS, TwoSBTS, and OneSBTS:

https://drive.google.com/open?id=0B_HzgsC9lP3nRURlaDUxQlJIOEk

As you can see in the video, the Destroy(gameObject); seems to do nothing, and the more buildings there are to spawn, the more likely that this causes problems. I would like to have 2, 4, and 6 as the values, but it rarely spawns correctly. Does anyone know how to fix this?

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

2 Replies

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

Answer by LucianoMacaDonati · Oct 26, 2016 at 10:30 PM

Hello @matthrewp,

You're calling Instantiate on an object marked for Destroy. I would make a function to Move the colliding building instead of re-spawning it.

My solution (keeping your original approach) would be as follows:

Create a function to only move the buildings to a new random location. On collision enter, call this function. Just in case, I would add a maximum amount of times you can call this and check for it every call before ultimately destroying the conflicting gameObject (to avoid callstack overflows).


Some other general tips for you:

I would start by removing the OnTriggerStay function so that it doesn't interfere with your debugging. Having two Debug.Log's that print "Called" makes it harder to know which function was called (and we only need to destroy once: when the overlap begins).

Second, I see you want to re-spawn the buildings that overlap others but you're actually calling Destroy on both buildings (both buildings collide with each other).

And lastly, the video shows only the first frame since you pause it and then press play. Wouldn't you want to see what happens the next frame? (Destroy waits until the end of the current loop to safely destroy the gameobject, more info here).

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 LucianoMacaDonati · Oct 27, 2016 at 12:34 AM 0
Share

@matthrewp have you read this at all ?

avatar image matthrewp · Oct 27, 2016 at 12:57 AM 0
Share

Thanks, I hadn't thought of moving the model ins$$anonymous$$d of destroying it!

avatar image LucianoMacaDonati matthrewp · Oct 27, 2016 at 01:04 AM 0
Share

The garbage collector will thank you for that :)

avatar image samf1111 · Mar 08, 2020 at 08:07 PM 0
Share

i would look for another solution so i don't lag.

avatar image
0

Answer by nathanthesnooper · Oct 26, 2016 at 09:58 PM

I have had a similar problem as well.

Some solutions I thought of: Destroy(this) instead of Destroy(gameObject)

The only solution that always worked for me was having a separate script delete things. Camera.main.gameObject.GetComponent<CustDelete>().Destroy(this);

CustDelete.cs:

using UnityEngine; using System.Collections public class CustDelete : MonoBehaviour { public void Destroy(GameObject des) { Destroy(des); } }

It is a Unity Bug I believe because it deletes the object before the script knows it ended (idk what actually happens)

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 matthrewp · Oct 26, 2016 at 10:04 PM 0
Share

I'm afraid trying this produced no result. Thanks for trying!

avatar image nathanthesnooper · Oct 26, 2016 at 10:17 PM 0
Share

Sorry :(, Hopefully you can find out... you know you shouldn't use the cube... it is collisions for your buildings and people might be able to see it for a split second.

avatar image Bunny83 · Oct 27, 2016 at 12:07 AM 1
Share

That actually makes no sense. Destroy is a static method so it doesn't matter in which scope you call it.

Also passing "this" to Destroy will only destroy the script instance but not the GameObject that containst the script.

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

61 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

Related Questions

Destroy function not documented? 2 Answers

Destroy On Contact 1 Answer

unet 5.2 client authority destroy 1 Answer

How to detect a single collision when 2 identical gameobjects collide. 1 Answer

My ship destroyed when i press fire 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