Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by stacker3d · Apr 01, 2015 at 04:40 PM · c#gameobjectdestroy

Why doesn't Destroy() show any effect on my GameObject?

I instantiate game objects any call immediately Destroy(), but the objects won't be removed from the hierarchy.

 GameObject go = GameObject.Instantiate( bullet, transform.position, Camera.main.transform.rotation ) as GameObject;
 Destroy(go,1);

The objects seem to be removed from the rendered frame but the hierarchy is filled with many instances "bullet(Clone)". Does it depend on the object the script is attached to? (I added the script to a dummy object, because there are no bullets in the scene when starting)

Full script:

 public class Bullet : MonoBehaviour {
 
     public GameObject launcher;
     public GameObject bullet; // prefab
 
     public GameObject platform; 
     private List<BulletDetail> bullets = new List<BulletDetail>();
 
     void Update ()
     {
         Debug.Log( "bullets Update");
         if ( bullets.Count < 10 ) {
             Transform transform = (Transform) Instantiate( bullet.transform );
             GameObject go = GameObject.Instantiate( bullet, transform.position, Camera.main.transform.rotation ) as GameObject;
             Destroy(go,1);
             HexGrid hexGrid = platform.GetComponent<HexGrid>();
             if ( hexGrid == null ) {
                 Debug.Log( "no hex grid selected");
                 return;
             }
             Debug.Log( "bullets.Count=" + bullets.Count);
             GameObject target = (GameObject) hexGrid.GetSelected();
             BulletDetail bull = new BulletDetail(go,transform,target.transform);
             bullets.Add( bull );
 
         }
         for(int i = bullets.Count - 1; i >=0 ; i--) {
             BulletDetail bd = bullets[i];
             if ( bd.Move() ) {
                 bullets.RemoveAt(i);
                 Destroy(bd.getBullet());
                 Debug.Log( "destroy called");
             }
         }
     }
 }

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
3
Best Answer

Answer by Xtro · Apr 01, 2015 at 07:08 PM

You are double Instantiating the bullet but destroying only once.

Remove the line 13 and use go.transform instead.

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 stacker3d · Apr 02, 2015 at 05:20 AM

I had to add a script to the prefab gameobject and apply it (Menu:GameObject/Apply Changes To Prefab):

 using UnityEngine;
 using System.Collections;
 
     public class Destroy : MonoBehaviour {
     
         public float timeToLive = 2f;
     
         // Use this for initialization
         void Start () {
             Destroy(gameObject, timeToLive);
         }
         
         // Update is called once per frame
         void Update () {
         
         }
     }


I would still like to know what is the reason that my approach from the question doesn't work (the gameObject is a parameter of Destroy() and should have a unique references.

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 Xtro · Apr 02, 2015 at 01:47 PM 0
Share

As I told you in my answer, you were creating 2 bullets at a time but destroying only one of them. You also marked my answer as a correct one. Why do you still wonder what was the reason?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroying objects 1 Answer

Destroy + Score 1 Answer

Simple on Collision Help (C#) 2 Answers

gameObject doesn't self-destruct after collision c# 2 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