• 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
0
Question by tiagorpg · Dec 20, 2016 at 07:33 AM · destroygameobject

Destroy(gameObject) not working

i cant destroy this object , the debug message is working and the powerup is spawned the array which stores de reference is being erased , as the explision ignores the rock after its exploded

 using UnityEngine;
 using System.Collections;
 
 public class Rock : MonoBehaviour {
     public GameObject powerUp;
     static Sprite []sprite;
     // Use this for initialization
     void Start () {
         if (sprite==null)
             sprite=Resources.LoadAll <Sprite>("Sprites/Rocks");
         int num =Random.Range(1,sprite.Length-1);
         transform.Find("Sprite").GetComponent<SpriteRenderer> ().sprite = sprite[num];
     }
     
     // Update is called once per frame
     void Update () {
     }    
     public void Explode()//<--- THAT LINE
     {
         Destroy(gameObject);
         Debug.Log("Exploded");
         if( powerUp!=null)
         {
             var pw=(GameObject)Instantiate(powerUp,transform.position,transform.rotation);
             int num=15;
             num=Random.Range(0,num);
             pw.GetComponent<PowerUp>().Init(num);
             
         }
     }
 }

this is the code that spawns the rock

 public void LoadMap(string name)
     {
         int type,depth=1;
         int i,j;
         string arena=INIWorker.IniReadValue("Arenas",mapName);
         if(arena!=null)
         {
             for(i=0;i<size;i++)
             {
                 for(j=0;j<size;j++)
                 {
                     type=int.Parse(arena.Substring(0,1));
                     //Debug.Log(" "+type);
                     if (type<materials.Length)
                     {
                         AllObjects[i,j]=(GameObject)Instantiate(materials[type],new Vector3(i+0f,j+0f,depth+0f),Quaternion.identity);
                     }
                     arena=arena.Substring(1,arena.Length-1);
                 }
             }
         }else Debug.Log(mapName+" nao encontrado");
     }


and this is the one the calls explode

 public void Check()
     {
         int x=(int)transform.position.x;
         int y=(int)transform.position.y;        
         if (MapLoader.CheckMapObject(x,y,"Rock"))
         {    Debug.Log(MapLoader.CheckMapObject(x,y,"Rock"));
             MapLoader.GetMapObject(x,y).transform.GetComponent<Rock>().Explode();
             Debug.Log(MapLoader.GetMapObject(x,y));
         }
         if (MapLoader.CheckMapObject(x,y,"Bomb"))
         {
             MapLoader.GetMapObject(x,y).transform.GetComponent<bomb>().Explode();
         }
             
     }


Comment
Add comment · Show 4
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 GrKl · Dec 20, 2016 at 10:56 AM 0
Share

On what object is your "Rock" class (script) attached to? Any error message?

In any case you should Destroy the object at the end of your Explode() function. As you are still using it in that function after your destroying it. You should have an error there.

as "transform.position" = "gameObject.transform.position" (same for rotation obviously)

So you can't "Destroy(gameObject)" then use that "gameObject"

avatar image tiagorpg · Dec 20, 2016 at 02:41 PM 0
Share

no error message and using destroy at any time makes no diference because the object is only destroyed in the next update, it was at the end before, but i put it in the begining exactly to avoid some error interrupting the script

avatar image GrKl tiagorpg · Dec 20, 2016 at 02:57 PM 0
Share

Explode is not an enumerator, the whole code inside Explode() should run during the same frame. The behavior your describing is odd. $$anonymous$$aybe others will have better info than I

avatar image tiagorpg GrKl · Dec 20, 2016 at 07:13 PM 0
Share

the code runs in the same frame, but Destroy() do not destroy in the frame it is invoked, only DestroyImmediate do this

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Main Camera problem between two scenes 0 Answers

Can't destroy my bullet 0 Answers

How to instantiate and destroy objects with 2d trigger 1 Answer

How to play audio clip when destroying an object? 2 Answers

UI Object is there but can't destroy it. 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